嵌入式linux上数据库,嵌入式linux下sqlite3数据库操作

本文展示了如何使用C语言进行SQLite数据库的基本操作,包括打开或创建数据库、创建数据表、插入数据和查询数据。示例代码中详细解释了sqlite3_open、sqlite3_exec、sqlite3_get_table等函数的用法,并通过实际例子演示了查询数据的两种方法。此外,还提到了编译时需要链接的库文件和路径设置。
摘要由CSDN通过智能技术生成

query.c

#include #include #include #include "sqlite3.h"

#define _DEBUG_

int main( void )

{

int i = 0 ;

int nrow = 0, ncolumn = 0;

char **azResult; //二维数组存放结果

sqlite3 *db=NULL;

char *zErrMsg = 0;

char value[200];

int id=10;

int ip=19;

int rc;

rc = sqlite3_open("aa.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");

//创建数据表

rc = sqlite3_exec(db,"create table mytable(id,ip);",NULL,NULL,zErrMsg);

//插入数据

memset(value,0,200);

//sprintf(value, "update tbl set id=%d,ip=%d",id,ip);//更新

sprintf(value, "insert into mytable(id,ip) values(%d,%d)",id,ip);//插入

sqlite3_exec(db,value,NULL,NULL,zErrMsg);

//查询数据

sqlite3_get_table( db , "SELECT * FROM mytable ", &azResult , &nrow , &ncolumn , &zErrMsg );

printf( "row:%d column=%d \n" , nrow , ncolumn );

printf( "The result of querying is : \n" );

for( i=ncolumn ; i

编译的时候要加上-lsqlite3,指定链接库。(gcc编译只需加-lsqlite3,arm-linux-gcc编译时除此之外还要添加-I /usr/include -L /home/sqlite3_test)

sqlite需要的库文件libsqlite3.so、libsqlite3.so.0、libsqlite3.so.0.8.6放在/home/sqlite3_test目录下,这三个库文件和虚拟机下的名字虽相同,却不能用,一定要是嵌入式移植的库文件。

程序最后运行结果为

上面是sqlite3数据库的基本操作(打开创建数据库,建立数据表,插入数据,查询数据)。

其中查询数据有两种操作方法,分别调用不同的函数,上面的方法比较简单,直接调用sqlite3_get_table()函数得到结果,还有一种方法如下面query1 .c所示。

query1.c

#include #include #include #include int Display(void *para,int n_column,char **column_value,char **column_name)

{

int i;

printf("记录包含 %d 个字段\n",n_column);

for( i = 0;i < n_column;i++)

{

printf("字段名:%s 字段值:%s\n",column_name[i],column_value[i]);

}

printf("------------------\n ");

return 0;

}

int main ()

{

char value[200];

sqlite3 *db;

char *errmsg = NULL;

int rc;

rc = sqlite3_open("bb.db",&db);

if(rc)

{

fprintf(stderr, "Can't open sqlite: %s\n", sqlite3_errmsg(db));

sqlite3_close(db);

exit(1);

}

else

printf("open sqlite success\n");

rc = sqlite3_exec(db,"create table mytable(id,ip);",NULL,NULL,errmsg);

if(rc == SQLITE_OK)

{

printf("create table success\n");

}

else

{

printf("create table failure\n");

}

sqlite3_exec(db,"insert into mytable(id,ip) values(1,2);",NULL,NULL,errmsg);

sqlite3_exec(db,"select * from mytable",Display,NULL,errmsg);

sqlite3_close(db);

}

运行结果如下,由于之前已经创建过mytable这个表,再次创建会提示创建失败。

查询采用了回显函数Display(void *para,int n_column,char **column_value,char **column_name)和sqlite3_exec(db,"select * from mytable",Display,NULL,errmsg);组合使用

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值