天天看點

Linux應用程式開發(一)---移植thttpd+Sqlite3+PHP5到arm linux(4)

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,參考文章:

移植thttpd Web伺服器到ARM-Linux系統

深入研究嵌入式web伺服器的視訊監控應用

輕量型thttpd+php5

Cross compiling thttpd-2.21b + php-5.2.1  

接上篇

【16】在開發闆終端進行測試

(1)建立資料庫檔案test.db

[[email protected] /]#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

[[email protected] www]#

sqlite資料庫操作測試

【17】在C代碼中進行操作測試

這裡以SQLite官方站點http://sqlite.org的quick start文檔中的測試程式為例對移植到ARM-Linux上的SQLite3進行測試。該程式清單如下:

//test_sqlite.c

#include <stdlib.h>

#include <stdio.h>

#include <sqlite3.h>

static int callback(void *NotUsed, int argc, char **argv, char **azColName)

{

   int i;

   for(i=0; i<argc;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、在上面建立的資料庫目錄下測試:

[[email protected] www]#./test_sqlite test.db "select * from stu"

id = 1

name = aa

age = 10

sex = m

id = 2

name = bb

age = 11

sex = f

[[email protected] www]#

【18】在php代碼中進行操作測試

thttpd+Sqlite3+PHP5綜合測試

(1)通路權限修改

開發闆/home/www/html目錄下的測試檔案目前隻有root使用者有通路權限,而客戶是以www使用者身份進行通路的,是以需要讓www使用者也具有通路權限

[[email protected] /]#chmod a+rx /home/www/html/test.php

[[email protected] /]#

test.php的内容如下:

<?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的檔案來完成:

通路:http://pear.php.net/go-pear

将整個網頁内容複制下來并存儲為go-pear.php即可。

我們這裡可以使用vim指令建立go-pear.php檔案,将内容複制進去,儲存,增加執行權限即可。

在開發闆終端用php指令執行go-pear.php:

[[email protected] /]#/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:

[[email protected] /]#/usr/local/bin/php go-pear.php

Could not open input file: go-pear.php

[[email protected] /]#

遺留問題:如何在交叉安裝php的pear擴充庫。

接下來,讓PHP5支援java在arm linux運作