#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
void do_insert(sqlite3* db)
{
int id;
char name[20]="";
float score;
printf("请输入要增加的id号,名字,分数\n");
scanf("%d %s %f",&id,name,&score);
char sql[128]="";
sprintf(sql,"insert into stu values(%d,'%s',%f);",id,name,score);
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return;
}
}
void do_delete(sqlite3* db)
{
int id;
char name[20]="";
float score;
printf("请输入要删除的id号\n");
scanf("%d",&id);
char sql[128]="";
sprintf(sql,"delete from stu where %d",id);
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return;
}
}
void do_update(sqlite3* db)
{
int id;
char name[20]="";
float score;
char data1[20],data2[20],data3[20],data4[20];
printf("请输入要修改的分类:id,name,score\n");
scanf("%s",data1);
printf("请输入修改分类下的的数值\n");
scanf("%s",data2);
printf("请输入要被修改数值\n");
scanf("%s",data3);
printf("修改完的数值是:");
scanf("%s",data4);
char sql[128]="";
sprintf(sql,"update stu set %s='%s' where %s='%s';",data3,data4,data1,data2);
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return;
}
}
void do_select(sqlite3* db)
{
int id;
char name[20]="";
float score;
printf("请输入要查找的id号\n");
scanf("%d",&id);
char sql[128]="";
sprintf(sql,"select * from stu where %d;",id);
char* errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return;
}
}
int main(int argc,const char *argv[])
{
//如果数据库不存在,则创建后打开
//如果存在直接打开
sqlite3* db = NULL;
if(sqlite3_open("./my.db",&db) != SQLITE_OK)
{
fprintf(stderr,"sqlite3_open :%s errcode:%d\n",sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
fprintf(stderr,"sqlite3_close :%s errcode:%d\n",sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
//创建一个表格
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,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
return -1;
}
printf("create table success\n");
char c = 0;
while(1)
{
system("clear");
printf("-------------------------\n");
printf("-------1.增--------------\n");
printf("-------2.删--------------\n");
printf("-------3.改--------------\n");
printf("-------4.查--------------\n");
printf("-------5.退出------------\n");
printf("-------------------------\n");
printf("请输入>>>");
c = getchar();
while(getchar()!=10);
switch(c)
{
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':
goto END;
default:
printf("输入错误请重新输入\n");
}
printf("输入任意字符清屏\n");
while(getchar()!=10);
}
END:
//关闭数据库
if(sqlite3_close(db)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_close :%s errcode:%d\n",sqlite3_errmsg(db),sqlite3_errcode(db));
return -1;
}
return 0;
}
sql实现增删改查
最新推荐文章于 2024-09-05 09:00:00 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)