参考:https://www.cnblogs.com/luego/p/11542420.html
https://blog.csdn.net/zouleideboke/article/details/73649886
sqlite3官网下载:https://www.sqlite.org/download.html
这里选择下载sqlite-amalgamation-3330000.zip(版本3.33.0)
将sqlite-amalgamation-3330000.zip解压,包含:shell.c、sqlite3.c、sqlite3.h、sqlite3ext.h几个文件
1、编译命令行管理工具
gcc shell.c sqlite3.c -lpthread -ldl -o sqlite3 //将生成sqlit3命令行管理工具
编译完成后生成可执行文件sqlite3,通过此工具可以使用命令行创建、打开、查看数据库文件,具体使用方法参考:
https://blog.csdn.net/zqixiao_09/article/details/50528181
https://blog.csdn.net/hhhhhyyyyy8/article/details/100587597
为方便使用,可在/bin目录下创建sqlite3的软链接,后续可以直接在命令行中输入sqlite3启用
ln -s /home/runoob/work/my_project/sqlite3/sqlite-amalgamation-3330000/sqlite3 sqlite3
2、编译libsqlite3.so共享库
gcc -g -shared -fPIC -c sqlite3.c
gcc -g -shared -fPIC -o libsqlite3.so sqlite3.o
应用程序可通过链接生成的libsqlite3.so来实现对数据库的管理(创建数据库、创建表格、插入数据、查询、数据、删除数据等)。下面以test.c为例(参考https://blog.csdn.net/zouleideboke/article/details/73649886)
1、test.c
/*********************************************************************************
* Copyright: (C) 2017 zoulei
* All rights reserved.
*
* Filename: insert.c
* Description: This file i
*
* Version: 1.0.0(2017年06月22日)
* Author: zoulei
* ChangeLog: 1, Release initial version on "2017年06月22日 19时31分12秒"
*
********************************************************************************/
#include
#include
#include "sqlite3.h"
#define _DEBUG_
int main(int argc, char**argv)
{
sqlite3 *db=NULL;
int len;
int i=0;
int nrow=0;
int ncolumn = 0;
char *zErrMsg =NULL;
char **azResult=NULL; //二维数组存放结果
/* 打开数据库 */
len = sqlite3_open("user",&db);
if( len )
{
/* fprintf函数格式化输出错误信息到指定的stderr文件流中 */
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));//sqlite3_errmsg(db)用以获得数据库打开错误码的英文描述。
sqlite3_close(db);
exit(1);
}
else printf("You have opened a sqlite3 database named user successfully!\n");
/* 创建表 */
char *sql = " CREATE TABLE SensorData(\
ID INTEDER PRIMARY KEY,\
SensorID INTEGER,\
siteNum INTEGER,\
Time VARCHAR(12),\
SensorParameter REAL\
);" ;
sqlite3_exec(db,sql,NULL,NULL,&zErrMsg);
#ifdef _DEBUG_
printf("%s\n",zErrMsg);
sqlite3_free(zErrMsg);
#endif
/*插入数据 */
char*sql1 ="INSERT INTO 'SensorData'VALUES(NULL,1,2,201430506201,13.5);";
sqlite3_exec(db,sql1,NULL,NULL,&zErrMsg);
char*sql2 ="INSERT INTO 'SensorData'VALUES(NULL,3,4,201530506302,14.5);";
sqlite3_exec(db,sql2,NULL,NULL,&zErrMsg);
char*sql3 ="INSERT INTO 'SensorData'VALUES(NULL,5,6,201630506413,18.6);";
sqlite3_exec(db,sql3,NULL,NULL,&zErrMsg);
/* 查询数据 */
sql="select *from SensorData";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
printf("nrow=%d ncolumn=%d\n",nrow,ncolumn);
printf("the result is:\n");
for(i=0;i<(nrow+1)*ncolumn;i++)
{
printf("azResult[%d]=%s\n",i,azResult[i]);
}
/* 删除某个特定的数据 */
sql="delete from SensorData where SensorID = 1 ;";
sqlite3_exec( db , sql , NULL , NULL , &zErrMsg );
#ifdef _DEBUG_
printf("zErrMsg = %s \n", zErrMsg);
sqlite3_free(zErrMsg);
#endif
/* 查询删除后的数据 */
sql = "SELECT * FROM SensorData ";
sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
printf( "row:%d column=%d\n " , nrow , ncolumn );
printf( "After deleting , the result is : \n" );
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
{
printf( "azResult[%d] = %s\n", i , azResult[i] );
}
sqlite3_free_table(azResult);
#ifdef _DEBUG_
printf("zErrMsg = %s \n", zErrMsg);
sqlite3_free(zErrMsg);
#endif
sqlite3_close(db);
return 0;
}
2、编译应用程序
gcc -g test.c -L. -lsqlite3 -lpthread -ldl -o test
执行./test程序,运行结果如下:
runoob@ubuntu:~/work/my_project/sqlite3/sqlite-amalgamation-3330000$ ./test
You have opened a sqlite3 database named user successfully!
table SensorData already exists
nrow=3 ncolumn=5
the result is:
azResult[0]=ID
azResult[1]=SensorID
azResult[2]=siteNum
azResult[3]=Time
azResult[4]=SensorParameter
azResult[5]=(null)
azResult[6]=1
azResult[7]=2
azResult[8]=201430506201
azResult[9]=13.5
azResult[10]=(null)
azResult[11]=3
azResult[12]=4
azResult[13]=201530506302
azResult[14]=14.5
azResult[15]=(null)
azResult[16]=5
azResult[17]=6
azResult[18]=201630506413
azResult[19]=18.6
zErrMsg = (null)
row:2 column=5
After deleting , the result is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = siteNum
azResult[3] = Time
azResult[4] = SensorParameter
azResult[5] = (null)
azResult[6] = 3
azResult[7] = 4
azResult[8] = 201530506302
azResult[9] = 14.5
azResult[10] = (null)
azResult[11] = 5
azResult[12] = 6
azResult[13] = 201630506413
azResult[14] = 18.6
zErrMsg = (null)