如何在Linux下用CC++语言 *** 作数据库sqlite3

如何在Linux下用CC++语言 *** 作数据库sqlite3,第1张

这里我们假设你已经编译好了sqlite的库文件 :

libsqlite3.a libsqlite3.la libsqlite3.so libsqlite3.so.0libsqlite3.so.0.8.6 pkgconfig

和可执行文件 : sqlite3

我们再假设你的sqlite3的安装目录在 /usr/local/sqlite3 目录下。

如果不是,我们可以这样做,将你的安装文件复制到 /usr/local/sqlite3 这个目录,

这样我们好在下面的 *** 作中更加统一,从而减少出错的概率

例如:[root@localhost home]# cp -rf sqlite-3.3.8-ix86//usr/local/sqlite3

这里假设 /home/sqlite-3.3.8-ix86/是你的安装目录,也就是说你的sqlite原来就是安装在这里

这样之后,我们的sqlite3的库文件目录是:/usr/local/sqlite3/lib

可执行文件 sqlite3 的目录是: /usr/local/sqlite3/bin

头文件 sqlite3.h 的目录是: /usr/local/sqlite3/include

好拉,现在开始我们的Linux下sqlite3编程之旅。

2. 开始

这里我们现在进行一个测试。

现在我们来写个C/C++程序,调用 sqlite 的 API 接口函数。

下面是一个C程序的例子,显示怎么使用 sqlite 的 C/C++ 接口.数据库的名字由第一个参数取得且第二个参数或更多的参数是 SQL 执行语句. 这个函数调用sqlite3_open() 在 16行打开数据库,并且sqlite3_close() 在 25 行关闭数据库连接。

[root@localhost temp]# vi opendbsqlite.c

按下 i 键切换到输入模式,输入下列代码:

// name: opendbsqlite.c

// This prog is used to test C/C++ API for sqlite3.It is verysimple,ha!

// Author : zieckey All rights reserved.

// data : 2006/11/13

#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

", sqlite3_errmsg(db))

sqlite3_close(db)

exit(1)

}

else printf("You have opened a sqlite3 database named zieckey.dbsuccessfully!

Congratulations! Have fun ! ^-^

")

sqlite3_close(db)//关闭数据库

return 0

}

退出,保存。(代码输入完成后,按下 Esc 键,然后输入: :wq ,回车就好拉)

好拉,现在编译:[root@localhost te

或者遇到这样的问题:

[root@localhost temp]# gcc opendbsqlite.c -o db.out

opendbsqlite.c:11:21: sqlite3.h: 没有那个文件或目录

opendbsqlite.c: In function `main':

opendbsqlite.c:19: `sqlite3' undeclared (first use in thisfunction)

opendbsqlite.c:19: (Each undeclared identifier is reported onlyonce

opendbsqlite.c:19: for each function it appears in.)

opendbsqlite.c:19: `db' undeclared (first use in thisfunction)

这是由于没有找到头文件的原因。

也许会碰到类似这样的问题:

[root@localhost temp]# gcc opendbsqlite.c -o db.out

/tmp/ccTkItnN.o(.text+0x2b): In function `main':

: undefined reference to `sqlite3_open'

/tmp/ccTkItnN.o(.text+0x45): In function `main':

: undefined reference to `sqlite3_errmsg'

/tmp/ccTkItnN.o(.text+0x67): In function `main':

: undefined reference to `sqlite3_close'

/tmp/ccTkItnN.o(.text+0x8f): In function `main':

: undefined reference to `sqlite3_close'

collect2: ld returned 1 exit status

这是个没有找到库文件的问题。

下面我们着手解决这些问题。

由于用到了用户自己的库文件,所用应该指明所用到的库,我们可以这样编译:

[root@localhost temp]# gcc opendbsqlite.c -o db.out-lsqlite3

我用用 -lsqlite3 选项就可以了(前面我们生成的库文件是 libsqlite3.so.0.8.6 等,

去掉前面的lib和后面的版本标志,就剩下 sqlite3 了所以是 -lsqlite3 )。

如果我们在编译安装的时候,选择了安装路径,例如这样的话:

.......

# ../sqlite/configure --prefix=/usr/local/sqlite3

# make

.......

这样编译安装时,sqlite的库文件将会生成在 /usr/local/sqlite3/lib 目录下

sqlite的头文件将会生成在 /usr/local/sqlite3/include 目录下

这时编译还要指定库文件路径,因为系统默认的路径没有包含 /usr/local/sqlite3/lib

[root@localhost temp]# gcc opendbsqlite.c -o db.out -lsqlite3-L/usr/local/sqlite3/lib

如果还不行的话,可能还需要指定头文件 sqlite3.h 的路径,如下:

[root@localhost temp]# gcc opendbsqlite.c -o db.out -lsqlite3-L/usr/local/sqlite3/lib -I/usr/l

这样编译应该就可以了 ,运行:

[root@localhost temp]# ./db.out

./db.out: error while loading shared libraries:libsqlite3.so.0: cannot open shared object file: No such file ordirectory

运行是也许会出现类似上面的错误。

这个问题因为刚刚编译的时候没有选择静态编译,那么按照默认的编译就动态编译的。

动态编译后,由于可执行文件在运行时要调用系统库文件,

那么沿着系统默认的库文件搜索路径搜索,就可能找不到我们现在所需的库文件。

致使出现 "error while loading shared libraries" 等错误。

我们可以这样解决:

方法一:静态编译

在编译时加上 -static 参数,例如

[root@localhost temp]# gcc opendbsqlite.c -o db.out -lsqlite3-L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include -static

[root@localhost temp]# ll

总用量 1584

-rwxr-xr-x 1 root root 1596988 11月 13 10:50 db.out

-rw-r--r-- 1 root root 614 11月 13 10:31 opendbsqlite.c

可以看到输出文件 db.out ,其大小为: 1596988k

运行,好了,没有出现错误

[root@localhost temp]# ./db.out

You have opened a sqlite3 database named zieckey.dbsuccessfully!

Congratulations! Have fun ! ^-^

方法二:重新配置系统环境变量 LD_LIBRARY_PATH

这时需要指定 libsqlite3.so.0 库文件的路径,也就是配置系统环境变量 LD_LIBRARY_PATH,

使系统能够找到 libsqlite3.so.0 。

去掉 -static ,在编译:

[root@localhost temp]# gcc opendbsqlite.c -o db.out -lsqlite3-L/usr/local/sqlite3/lib -I/usr/local/sqlite3/include

[root@localhost temp]# ll

总用量 36

-rwxr-xr-x 1 root root 12716 11月 13 10:56 db.out

-rw-r--r-- 1 root root 614 11月 13 10:31 opendbsqlite.c

[root@localhost temp]#

可以看到输出文件 db.out ,其大小为: 12716k,比刚才的静态编译要小得多。

所以我们推荐使用动态编译的方法。

好了,现在我们来指定系统环境变量 LD_LIBRARY_PATH 的值

在shell下输入:

[root@localhost temp]# exportLD_LIBRARY_PATH=/usr/local/sqlite3/lib:$LD_LIBRARY_PATH

再运行

[root@localhost temp]# ./db.out

You have opened a sqlite3 database named zieckey.dbsuccessfully!

Congratulations! Have fun ! ^-^

下载一个Linux 的sqlite3工具(或到SqLite网站下载sqlite的源代码,在你的系统中编译出sqlite3工具),用这个工具打开:

sqlite3 /tmp/test.db

你不需要做任何的配置来使其工作。所有你需要的就是安装它并开始使用它。test -z "/usr/local/bin" || mkdir -p -- "/usr/local/bin" ./libtool --mode=install /usr/bin/install -c sqlite3 /usr/local/bin/sqlite3 /usr/bin/install -c .libs/sqlite3 /usr/local/bin/sqlite3 test -z "/usr/local/include" || mkdir -p -- "/usr/local/include" /usr/bin/install -c -m 644 'sqlite3.h' '/usr/local/include/sqlite3.h' /usr/bin/install -c -m 644 'sqlite3ext.h' '/usr/local/include/sqlite3ext.h' test -z "/usr/local/share/man/man1" || mkdir -p -- "/usr/local/share/man/man1" /usr/bin/install -c -m 644 './sqlite3.1' '/usr/local/share/man/man1/sqlite3.1' test -z "/usr/local/lib/pkgconfig" || mkdir -p -- "/usr/local/lib/pkgconfig" /usr/bin/install -c -m 644 'sqlite3.pc' '/usr/local/lib/pkgconfig/sqlite3.pc'提示:如果你对mysql数据库有兴趣,你也可以安装在你的系统中。原文:SQLite3 is an extremely lightweight SQL database engine that is self-contained and serverless.There is absolutely no configuration that you need to do to get it working. All you need to do is–install it, and start using it.Since this is serverless, it is used in lot of the famous software that you are using, and you probably didn’t even know those software were using it. View this list to see all the big name companies who are using SQLite. PHP programming language has SQLite database built in.If you’ve never used SQLite, follow the steps mentioned in this article to install it on Linux, and create a sample database.


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/7622855.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-07
下一篇 2023-04-07

发表评论

登录后才能评论

评论列表(0条)

保存