//在Ubuntu搭建环境
apt-get install sqlite3
apt-get install libsqlite3-dev
//创建表 CREATE TABLE "stu" ( "id" INTEGER PRIMARY KEY NOT NULL, "name" TEXT NOT NULL, "phone num" INTEGER NOT NULL, "gid" INTEGER NOT NULL ) ; //插入数据 INSERT INTO "stu" values(20200812, '王小强', '1361111122',1); INSERT INTO "stu" values(2020082, '王二强', '1361111122',1); INSERT INTO "stu" values(20200811, '王大强', '1361111122',1);
测试程序,编译记得加参数 -lsqlite3
//测试程序 #include <stdio.h> #include "sqlite3.h" #include <string.h> #include <stdlib.h> #include <unistd.h> //每条记录调用一次 int showinfo(void *argc,int col,char **str1,char **str2) { int i = 0; for(i=0; i<col; i++) { printf("%s = %s ",*(str2+i),*(str1+i)); } printf(" "); return 0; } int main(int argc,char** argv) { if(argc < 3) { printf("参数不正确 "); return 0; } int ret; char name[20]; int age; sqlite3 *mydb; //定义句柄 //打开、新建数据库文件 ret=sqlite3_open(argv[1],&mydb); if(ret!=SQLITE_OK) { printf("打开,新建数据库失败! "); return -1; } ret=sqlite3_exec(mydb,argv[2],showinfo,&ret,NULL); if(ret!=SQLITE_OK) { printf("加入数据失败! "); return -1; } sqlite3_close(mydb); return 0; }