例:
1.打开一个函数库文件
ret = sqlite3_open("student.db",&pDb);
if(ret != SQLITE_OK)
fprintf (stderr,"fail to sqlite3_open :%s\n",sqlite3_errmsg(pDb));
return -1;
2.执行SQL语句
(1)创建列表
sprintf(tmpbuff,"create table if not exists student(id integer primary key asc,name text,sex text,age integer,score double;");
ret = sqlite3_exec(pDb,tmpbuff,NULL,NULL,&perrmsg);
if(ret != SQLITE_OK)
fprintf(stderr,"sqlite3_exec failed!:%s\n",perrmsg);
sqlite3_free(perrmsg);
sqlite3_close(pDb);
return -1;
(2)加入元素
利用for循环将结构体中的内容填入表中
sprintf(tmpbuff,"insert into student
values(NULL,\"%s\",\"%s\",%d,%lf);",s[i].name,s[i].sex,s[i].age,s[i].score);
ret = sqlite3_exec(pDb,tmpbuff,NULL,NULL,&perrmsg);
if(ret != SQLITE_OK)
fprintf(stderr,"sqlite3_exec failed!:%s\n",perrmsg);
sqlite3_free(perrmsg);
sqlite3_close(pDb);
return -1;
(3)查看列表内容
sprintf(tmpbuff,"select * from student;");
ret = sqlite3_exec(pDb,tmpbuff,callback,NULL,&perrmsg); //调用callback函数
if(ret != SQLITE_OK)
fprintf(stderr,"sqlite3_exec failed!:%s\n",perrmsg);
sqlite3_free(perrmsg);
sqlite3_close(pDb);
return -1;
函数callback(遍历表格)打印到终端
3.关闭数据库