sqlite3实现表格操作

#include <mystdio.h>

int do_insert(sqlite3* db);

int do_delete(sqlite3* db);

int do_update(sqlite3* db);

int do_select(sqlite3* db);

int main(int argc, const char *argv[])

{

sqlite3* db;

if(sqlite3_open("./sq.db",&db) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_open:%s %d\n",sqlite3_errmsg(db),LINE);

return -1;

}

printf("sq.db open success\n");

//创建表格

char sql[128] = "create table if not exists stu(id int,name char,score float);";

char* errmsg = NULL;

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,LINE);

return -1;

}

printf("create success\n");

//增删改查

int chooes;

int flags = 0;

while(1)

{

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

printf("------1.插入-------\n");

printf("------2.删除-------\n");

printf("------3.修改-------\n");

printf("------4.查询-------\n");

printf("------5.退出-------\n");

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

printf("请输入>>>");

scanf("%d",&chooes);

while(getchar() != '\n');

switch(chooes)

{

case 1:

do_insert(db);

break;

case 2:

do_delete(db);

break;

case 3:

do_update(db);

break;

case 4:

do_select(db);

break;

case 5:

flags=1;

break;

default:

printf("输入错误,请重新输入\n");

}

if(flags)

break;

printf("输入任意字符清屏>>>");

while(getchar() != '\n');

}

if(sqlite3_close(db) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_close failed %d\n",LINE);

return -1;

}

printf("sq.db close success\n");

return 0;

}

/*

* function: 插入数据

* @param [ in]

* @param [out]

* @return

*/

int do_insert(sqlite3* db)

{

int id;

char name[20]="";

float score;

printf("请输入id>>> ");

scanf("%d",&id);

getchar();

printf("请输入name>>> ");

scanf("%s",name);

getchar();

printf("请输入score>>> ");

scanf("%g",&score);

getchar();

char sql[128]="";

sprintf(sql,"insert into stu values(%d,\"%s\",%g);",id,name,score);

printf("sql = %s\n",sql);

char* errmsg;

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,LINE);

return -1;

}

printf("inert success\n");

return 0;

}

/*

* function: 删除数据

* @param [ in]

* @param [out]

* @return

*/

int do_delete(sqlite3* db)

{

int id;

printf("请输入要删除的id>>>");

scanf("%d",&id);

char sql[128] ="";

sprintf(sql,"delete from stu where id=%d;",id);

char* errmsg;

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,LINE);

return -1;

}

printf("delete success\n");

return 0;

}

/*

* function: 修改数据

* @param [ in]

* @param [out]

* @return

*/

int do_update(sqlite3* db)

{

char buf[20] ="";

printf("请输入要修改的字段>>>");

scanf("%s",buf);

if(strcmp(buf,"id") == 0 )

{

int id;

int fid;

printf("请输入要修改的id>>>");

scanf("%d",&id);

printf("请输入要修改成的id值>>>");

scanf("%d",&fid);

char sql[128] ="";

sprintf(sql,"update stu set id=%d where id=%d;",fid,id);

char* errmsg;

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,LINE);

return -1;

}

printf("update success\n");

}

else if(strcmp(buf,"name") == 0 )

{

char arr[20]="";

char brr[20]="";

printf("请输入要修改的name>>>");

scanf("%s",arr);

printf("请输入要修改成的name值>>>");

scanf("%s",brr);

char sql[128] ="";

sprintf(sql,"update stu set name=%s where name=%s;",brr,arr);

char* errmsg;

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,LINE);

return -1;

}

printf("update success\n");

}

else if(strcmp(buf,"score") == 0 )

{

float sre;

float src;

printf("请输入要修改的score>>>");

scanf("%g",&sre);

printf("请输入要修改成的score值>>>");

scanf("%g",&src);

char sql[128] ="";

sprintf(sql,"update stu set score=%g where score=%g;",src,sre);

char* errmsg;

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_exec:%s %d\n",errmsg,LINE);

return -1;

}

printf("update success\n");

}

else

{

printf("输入错误,格式不正确\n");

}

}

/*

* function: 查询数据

* @param [ in]

* @param [out]

* @return

*/

int do_select(sqlite3* db)

{

char sql[128] = "select * from stu;";

char* errmsg=NULL;

int flag = 0;

char** pres = NULL;

int row,column;

if(sqlite3_get_table(db,sql,&pres,&row,&column,&errmsg) != SQLITE_OK)

{

fprintf(stderr,"sqlite3_get_table:%s %d\n",errmsg,LINE);

return -1;

}

printf("selete stu success\n");

for(int i=0;i<(row+1)*column;i++)

{

printf("%s\t",pres[i]);

if((i+1)%column == 0)

putchar(10);

}

return 0;

}

相关推荐
Yang.9912 小时前
基于Windows系统用C++做一个点名工具
c++·windows·sql·visual studio code·sqlite3
yy_xzz24 天前
LINUX下使用SQLite查看.db数据库文件
sqlite3
Rookie也要加油2 个月前
02SQLite
数据库·sqlite3
zybsjn2 个月前
SSH 远程连接到 Linux 服务器上的 SQLite
linux·服务器·ssh·sqlite3
※※冰馨※※3 个月前
SQLite 插入一行并返回主键
数据库·oracle·sqlite3
※※冰馨※※3 个月前
SQLite 插入数据并返回自增ID
数据库·c#·sqlite3
※※冰馨※※4 个月前
如何在SQLite中实现自动时间戳
开发语言·数据库·sqlite3
真果粒wrdms4 个月前
【在线词典】项目实现
linux·c语言·嵌入式硬件·算法·udp·sqlite3
破无差6 个月前
Python sqlite3库 实现 数据库基础及应用 输入地点,可输出该地点的爱国主义教育基地名称和批次的查询结果。
数据库·python·sqlite3