在 Solaris 10 系统上编译并使用SQLite-3.4.2

作者: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
2.1 编译sqlite3的命令行工具文件
它是一个使用sqlite库去操作sqlite数据库文件的独立文件
编译它需要一个 shell.c 文件
这个文件可以从 http://www.sqlite.org/cvstrac/fileview?f=sqlite/src/shell.c&v=1.166
处得到一个拷贝,好了,命令如下:
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”。

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能