sqlite3 C安装调用

sqlite3 C配置调用

1.下载

从官网下载源码下载,里面只是几个头文件,源文件就够了。将源文件,头文件放在放在项目的根目录,放在并且新建一个include文件夹。

2.编写程序

connect.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <sqlite3.c>

int callback(void * agrument,int count,char** result,char **name){
for(int i = 0;i < count;i++){
printf("%s\r\n",result[i]);
}
}

int main(){
sqlite3 *ppDb;
if (sqlite3_open("demo.db",&ppDb) == 1){
printf("database opened failed\n");
exit(0);
}
printf("database opened success\n");

char *errmsg;

if(sqlite3_exec(ppDb,"create table demo(id integer primary key autoincrement,name varchar(100))",NULL,NULL,&errmsg) == 1){
printf("%s",errmsg);
exit(0);
}
printf("creat table successfully\n");

if(sqlite3_exec(ppDb,"insert into demo (name) values(\"1\")",NULL,NULL,&errmsg) == 1){
printf("%s\n",errmsg);
exit(0);
}
printf("insert data successfully\n");


if(sqlite3_exec(ppDb,"select * from demo",callback,NULL,&errmsg) == 1){
printf("%s\n",errmsg);
exit(0);
}

sqlite3_close(ppDb);

return 0;
}

3.运行

1
gcc connect.c -o connect -Iinclude -std=c99

4.解释

sqlite3 结构体,数据库对象。sqlite3_open(char * filename,sqlite3 ** db ) 。参数一,数据库名,若不存在就创建。返回1,出错;返回0,成功,返回样值sqlite3_exec(sqlite3 *,const char * sql,int ( * callback)(void *,int,char **,char **),void *,char ** errmsg) ,第一个参数,数据库对象;第二个参数sql语句,第三个回调函数(回调函数传过来的参数,数据列个数,数据结果一维数组,表结构名称一维数组),第三个参数传递给回调参数,第四个参数错误异常信息。sqlite3_close(sqlite3 *) ,释放链接。

sqlite官网