作者:zieckey (zieckey@yahoo.com.cn)
All Rights Reserved!
1、引言
其实Solaris 10 上自带了SQLite,其可执行文件是 /lib/svc/bin/sqlite
这里就可以测试下:
bash-3.00# /lib/svc/bin/sqlite
SQLite version 2.8.15-repcached-Generic Patch
Enter ".help" for instructions
sqlite>
发现这个版本有点老,2.8.15
但是我通过find命令并没有找到SQLite的动态共享库,
所以我决定自己编译一个最新的SQLite
2、下载
在SQLite的官方网址
http://www.sqlite.org/download.html 下载,
这里我下载了源码的这个包:sqlite-amalgamation-3_4_2.zip
该包将SQLite数据库的所有源代码集中在一个 sqlite3.c C源文件和 sqlite3.h 头文件,
这样就非常便于我们编译,特别是对于Mikefile文件规则不熟悉的人。
3、编译
bash-3.00# cd sqlite/
bash-3.00# ls
sqlite3.c sqlite3.h
bash-3.00# gcc shell.c sqlite3.c -o sqlite3
bash-3.00# ls
shell.c sqlite3 sqlite3.c sqlite3.h
输出文件也就是SQLite3的命令行工具文件被命名为:sqlite3
2.2 编译动态共享库文件
bash-3.00# gcc -c sqlite3.c -o libsqlite3.so
bash-3.00# ls
libsqlite3.so shell.c sqlite3 sqlite3.c sqlite3.h
输出文件也就是SQLite3的动态链接库文件被命名为:libsqlite3.so
4、测试:
4.1 SQLite3的命令行工具测试
继续在当前目录下进行
bash-3.00# ./sqlite3 zieckey.db
SQLite version 3.4.2
Enter ".help" for instructions
sqlite> .exit
bash-3.00#
可以看到上面是SQLite3的命令行工具执行任务成功
4.2 编写C测试程序测试动态链接库文件
这里我给出一个简单的程序:
|
/* ** name :testsqlite.c ** This file is used to test C/C++ API for sqlite ** Author : zieckey ** Date : 2006/04/11 */ #include <stdio.h> #include <sqlite3.h> int main( void ) { sqlite3 *db=NULL; char *zErrMsg = 0; int rc; rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
if( rc ){ fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } else printf("open zieckey.db successfully!\n"); sqlite3_close(db); //关闭数据库
return 0; }
|
上面C程序保存为 testsqlite.c 文件,
bash-3.00# gcc testsqlite.c
testsqlite.c:8:21: sqlite3.h: 无此文件或目录
testsqlite.c: In function `main':
testsqlite.c:12: error: `sqlite3' undeclared (first use in this function)
testsqlite.c:12: error: (Each undeclared identifier is reported only once
testsqlite.c:12: error: for each function it appears in.)
testsqlite.c:12: error: `db' undeclared (first use in this function)
testsqlite.c:25:2: warning: no newline at end of file
bash-3.00# gcc testsqlite.c -lsqlite3
testsqlite.c:8:21: sqlite3.h: 无此文件或目录
testsqlite.c: In function `main':
testsqlite.c:12: error: `sqlite3' undeclared (first use in this function)
testsqlite.c:12: error: (Each undeclared identifier is reported only once
testsqlite.c:12: error: for each function it appears in.)
testsqlite.c:12: error: `db' undeclared (first use in this function)
testsqlite.c:25:2: warning: no newline at end of file
bash-3.00# gcc testsqlite.c -lsqlite3 -I.
testsqlite.c:25:2: warning: no newline at end of file
ld: 致命的: 库 -lsqlite3: 没有找到
ld: 致命的: 文件处理错误。无输出写到a.out
collect2: ld returned 1 exit status
bash-3.00# gcc testsqlite.c -lsqlite3 -I. -L.
testsqlite.c:25:2: warning: no newline at end of file
bash-3.00# ls
a.out libsqlite3.so shell.c sqlite3 sqlite3.c sqlite3.h testsqlite.c zieckey.db
bash-3.00# ./a.out
open zieckey.db successfully!
好了终于成功了!恭喜你!
关于上面的一些错误问题的解决办法的详细解释见“参考文档1”。
发表于 @ 2007年08月19日 23:46:00|评论(loading...)|编辑