Linux SQLite3基本使用

工作原理:

不像常见的客户-服务器范例,SQLite引擎不是个程序与之通信的独立进程,而是连接到程序中成为它的一个主要

部分。所以主要的通信协议是在编程语言内的直接API调用。

SQLite是一个轻量级的数据库。和常用的MySQL大同小异。和MySQL相比,数据类型基本一样,只是sqlite的指令

都是以"."开头(如:mysql中查看当前有哪些数据库是“show databases",而sqlite中是".databases")。

当然,SQL语句都是一样的语法规则。

关于安装:

sqlite主页http://www.sqlite.org/.跳转到下载也http://www.sqlite.org/download.html。源码下载sqlite-amalgamation-3.7.3.tar.gz

解压后生成sqlite-3.7.3目录. cd 进入sqlite-3.7.3。

./configure
make
sudo make install   安装完成。

测试:

在任意目录下新建一个数据库,比如student ,

命令: sqlite3 student

出现如下提示:

SQLite version 3.7.2
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>

输入.help可以看到命令列表。

输入sql语句create table user(username text primary key, password text); 建一张user表

输入sql语句insert into user values("zhangsan", "123"); 插入一个用户。

输入sql语句select * from user; 可以查看user表.

输入sql命令是记得结尾的';'号。


关于移植到ARM端:

下载源码,并解压。

进入解压完成的目录,输入命令

./configure --prefix=/home/Ciel/fl2440/sqlite --host=arm-linux  CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc

配置参数--prefix=/home/Ciel/fl2440/sqlite指明编译后文件的安装路径

配置参数--host=arm-linux指明运行的系统平台

配置参数CC=/opt/buildroot-2012.08/arm920t/usr/bin/arm-linux-gcc指明交叉编译器的路径

make 

make install

将得到的库 :libsqlite3.so.0.8.6 ,libsqlite3.so.0, libsqlite3.so 和 sqlite3 压缩打包传到你的ARM开发板上。

libsqlite3.so.0.8.6 ,libsqlite3.so.0, libsqlite3.so 放到 /lib ,sqlite3 放到 /bin

在Linux下 C 操作:

下列是简单的应用程序:

#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("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
sqlite3_close(db); //关闭数据库
return 0;
}

编译:[root@localhost Ciel]# 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 this function)
opendbsqlite.c:19: (Each undeclared identifier is reported only once
opendbsqlite.c:19: for each function it appears in.)
opendbsqlite.c:19: `db' undeclared (first use in this function)

②没有找到库文件的原因

/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 Ciel]# gcc opendbsqlite.c -o db.out -lsqlite3 

-L/usr/local/sqlite3/lib  //指定库文件的位置

-I/usr/local/sqlite3/include //指定头文件的位置

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值