linux独立应用程序开发,Linux应用程序开发(一)
Linux應用程序開發(一)---移植thttpd+Sqlite3+PHP5到arm linux(4)
移植環境(紅色粗字體字為修改后內容,藍色粗體字為特別注意內容)
1,主機環境:VMare下CentOS 5.5 ,1G內存。
2,集成開發環境:Elipse IDE
3,編譯編譯環境:arm-linux-gcc v4.4.3,arm-none-linux-gnueabi-gcc v4.5.1。
4,開發板:mini2440,2M nor flash,128M nand flash。
5,u-boot版本:u-boot-2009.08
6,linux 版本:linux-2.6.32.2
7,參考文章:
接上篇
【16】在開發板終端進行測試
(1)創建數據庫文件test.db
[root@mini2440 /]#cd /home/www
#sqlite3 test.db
SQLite version 3.7.7.1 2011-06-28 17:39:05
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
(2)創建表
sqlite> create table students(id integer,name text,age integer);
sqlite> .tables
students
sqlite>
(3)刪除表
sqlite> drop table students
sqlite> .tables
sqlite>
(4)查看表結構
sqlite> create table students(id integer,name text,age integer);
sqlite> .schema students
CREATE TABLE students(id integer,name text,age integer);
sqlite>
(5)插入列
sqlite> alter table students add cul;
sqlite> alter table students add column sex text;
sqlite> .schema students
CREATE TABLE students(id integer,name text,age integer, cul, sex text);
sqlite>
(6)插入表記錄
sqlite> insert into students values(1,'aa',10,0,'m');
sqlite> insert into students values(2,'bb',11,1,'f');
sqlite> select * from students;
1|aa|10|0|m
2|bb|11|1|f
sqlite>
(7)重命名表
sqlite> alter table students rename to stu;
sqlite>
(8)刪除某一列,這為列cul
sqlite> begin transaction;
sqlite> create temporary table stu_bak(id integer,name text,age integer,sex text);
sqlite> insert into stu_bak select id,name,age,sex from stu;
sqlite> drop table stu;
sqlite> create table stu(id integer,name text,age integer,sex text);
sqlite> insert into stu select id,name,age,sex from stu_bak;
sqlite> drop table stu_bak;
sqlite> select * from stu;
1|aa|10|m
2|bb|11|f
sqlite> commit;
sqlite>
(9)退出程序
sqlite> .quit
[root@mini2440 www]#
sqlite數據庫操作測試
【17】在C代碼中進行操作測試
這里以SQLite官方站點http://sqlite.org的quick start文檔中的測試程序為例對移植到ARM-Linux上的SQLite3進行測試。該程序清單如下:
//test_sqlite.c
#include
#include
#include
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
int i;
for(i=0; i
{
printf("%s = %s\n", azColName[i], argv [i]);
}
printf("\n");
return 0;
}
int main(int argc, char **argv)
{
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=3 )
{
fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
}
rc = sqlite3_open(argv[1], &db);
if( rc )
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
}
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
if( rc!=SQLITE_OK )
{
fprintf(stderr, "SQL error: %s\n", zErrMsg);
}
sqlite3_close(db);
return 0;
}
使用如下命令編譯測試程序:
交叉編譯時采用arm-linux-gcc -I /……(安裝路徑)/include -L/……(安裝路徑)/lib -o target src -lsqlite3
arm-linux-gcc -o test_sqlite test_sqlite.c -lsqlite3 -L /nfsboot/rootfs/usr/local/lib/ -I /nfsboot/rootfs/usr/local/include/
3、在上面新建的數據庫目錄下測試:
[root@mini2440 www]#./test_sqlite test.db "select * from stu"
id = 1
name = aa
age = 10
sex = m
id = 2
name = bb
age = 11
sex = f
[root@mini2440 www]#
【18】在php代碼中進行操作測試
thttpd+Sqlite3+PHP5綜合測試
(1)訪問權限修改
開發板/home/www/html目錄下的測試文件目前只有root用戶有訪問權限,而客戶是以www用戶身份進行訪問的,因此需要讓www用戶也具有訪問權限
[root@mini2440 /]#chmod a+rx /home/www/html/test.php
[root@mini2440 /]#
test.php的內容如下:
dl('sqlite3.so');
$dbh = new PDO('sqlite:test.db');
if ($dbh)
{
$dbh->beginTransaction();
$sth = $dbh->prepare('select * from stu');
$sth->execute();
$result = $sth->fetchAll();
print_r($result);
}
else
{
echo "Err \n";
}
?>
在瀏覽器中鍵入開發板IP地址http://10.1.0.129/test.php,顯示內容如下:
Array ( [0] => Array ( [id] => 1 [0] => 1 [name] => aa
[1] => aa [age] => 10 [2] => 10 [sex] => m [3] => m ) [1]
=> Array ( [id] => 2 [0] => 2 [name] => bb [1] => bb
[age] => 11 [2] => 11 [sex] => f [3] => f ) )
終于在瀏覽器中看到了數據庫test.db中的內容。這說明?thttpd+Sqlite3+PHP5已經在正常工作了。
【19】在php中安裝pear擴展庫
安裝pear需要使用php命令來執行一個go-pear.php的文件來完成:
將整個網頁內容復制下來并存儲為go-pear.php即可。
我們這里可以使用vim命令建立go-pear.php文件,將內容復制進去,保存,增加執行權限即可。
在開發板終端用php命令執行go-pear.php:
[root@mini2440 /]#/usr/local/bin/php go-pear.php
PHP Warning:? Module 'PDO' already loaded in Unknown on line 0
PHP Warning:? Module 'pdo_sqlite' already loaded in Unknown on line 0
PHP Warning:? Module 'SQLite' already loaded in Unknown on line 0
Could not open input file: go-pear.php
出現上面錯誤的原因是,外部動態庫加載有兩種方式,一種是通過編譯指定 -ldl
將外部extension目錄直接編譯進php,另一種是通過php的ini文件指定,這里的錯誤指示出外部動態鏈接庫已經被編譯進php了,還在
php.ini指定,因而出錯。打開php.ini文件,注釋到下面幾行(參考http://www.somacon.com/p520.php)
; Directory in which the loadable extensions (modules) reside.
extension_dir = "/usr/local/lib/php/extensions/no-debug-non-zts-20060613"
;extension=pdo.so
;extension=pdo_sqlite.so
;extension=sqlite.so
然后再開發板終端用php命令執行go-pear.php:
[root@mini2440 /]#/usr/local/bin/php go-pear.php
Could not open input file: go-pear.php
[root@mini2440 /]#
遺留問題:如何在交叉安裝php的pear擴展庫。
接下來,讓PHP5支持java在arm linux運行
總結
以上是生活随笔為你收集整理的linux独立应用程序开发,Linux应用程序开发(一)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux 读取大量图片 内存,10 张
- 下一篇: 红旗linux桌面版反应慢,红旗Linu