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

[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 <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、在上面新建的数据库目录下测试:
[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的内容如下:

<?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:

[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运行
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值