数据库的增删改:
代码
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
int do_insert(sqlite3 *db);
int do_delete(sqlite3 *db);
int do_update(sqlite3 *db);
int main(int argc, const char *argv[])
{
sqlite3 *db=NULL;
if(sqlite3_open("./dict.db",&db)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%d:%s\n",\
__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
return -1;
}
printf("sqlite3_open success\n");
//创建一个表格
char sql[128]="create table if not exists stu (id int primary key,name char,score float);";
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("table stu create success\n");
char choose=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("请输入>>>");
choose=getchar();
while(getchar()!=10);
switch(choose)
{
case '1':
do_insert(db);
break;
case '2':
do_delete(db);
break;
case '3':
do_update(db);
break;
case '4':
break;
case '5':
goto END;
break;
default:
printf("输入错误\n");
}
printf("输入任意字符清屏>>>");
while(getchar()!=10);
}
END:
if(sqlite3_close(db)!=0)
{
fprintf(stderr,"line: %d sqlite3_close:%d:%s\n",\
__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
return -1;
}
return 0;
}
int do_insert(sqlite3 *db)
{
char sql[128]="";
int id;
char name[20]="";
float score;
printf("请输入id>>>");
scanf("%d",&id);
while(getchar()!=10);
printf("请输入姓名>>>");
scanf("%s",name);
while(getchar()!=10);
printf("请输入成绩>>>");
scanf("%f",&score);
while(getchar()!=10);
sprintf(sql,"insert into stu values(%d,'%s',%g)",id,name,score);
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("insert success\n");
}
int do_delete(sqlite3 *db)
{
char sql[128]="";
int id;
printf("请输入要删除的id>>>");
scanf("%d",&id);
while(getchar()!=10);
sprintf(sql,"delete from stu where id=%d",id);
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("delete success\n");
}
int do_update(sqlite3 *db)
{
int id;
char name[20]="";
float score;
char sql[128]="";
char c=0;
char *errmsg=NULL;
printf("请输入要修改的id>>>");
scanf("%d",&id);
while(getchar()!=10);
printf("修改姓名选1,修改成绩选2>>>");
c=getchar();
while(getchar()!=10);
switch(c)
{
case '1':
printf("输入要修改的姓名>>>");
scanf("%s",name);
while(getchar()!=10);
sprintf(sql,"update stu set name=%s where id=%d",name,id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("update success\n");
goto END;
case '2':
printf("请输入要修改的成绩>>>");
scanf("%f",&score);
while(getchar()!=10);
sprintf(sql,"update stu set score=%f where id=%d",score,id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
goto END;
}
END:
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("update success\n");
}
#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
int do_insert(sqlite3 *db);
int do_delete(sqlite3 *db);
int do_update(sqlite3 *db);
int main(int argc, const char *argv[])
{
sqlite3 *db=NULL;
if(sqlite3_open("./dict.db",&db)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%d:%s\n",\
__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
return -1;
}
printf("sqlite3_open success\n");
//创建一个表格
char sql[128]="create table if not exists stu (id int primary key,name char,score float);";
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("table stu create success\n");
char choose=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("请输入>>>");
choose=getchar();
while(getchar()!=10);
switch(choose)
{
case '1':
do_insert(db);
break;
case '2':
do_delete(db);
break;
case '3':
do_update(db);
break;
case '4':
break;
case '5':
goto END;
break;
default:
printf("输入错误\n");
}
printf("输入任意字符清屏>>>");
while(getchar()!=10);
}
END:
if(sqlite3_close(db)!=0)
{
fprintf(stderr,"line: %d sqlite3_close:%d:%s\n",\
__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
return -1;
}
return 0;
}
int do_insert(sqlite3 *db)
{
char sql[128]="";
int id;
char name[20]="";
float score;
printf("请输入id>>>");
scanf("%d",&id);
while(getchar()!=10);
printf("请输入姓名>>>");
scanf("%s",name);
while(getchar()!=10);
printf("请输入成绩>>>");
scanf("%f",&score);
while(getchar()!=10);
sprintf(sql,"insert into stu values(%d,'%s',%g)",id,name,score);
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("insert success\n");
}
int do_delete(sqlite3 *db)
{
char sql[128]="";
int id;
printf("请输入要删除的id>>>");
scanf("%d",&id);
while(getchar()!=10);
sprintf(sql,"delete from stu where id=%d",id);
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("delete success\n");
}
int do_update(sqlite3 *db)
{
int id;
char name[20]="";
float score;
char sql[128]="";
char c=0;
char *errmsg=NULL;
printf("请输入要修改的id>>>");
scanf("%d",&id);
while(getchar()!=10);
printf("修改姓名选1,修改成绩选2>>>");
c=getchar();
while(getchar()!=10);
switch(c)
{
case '1':
printf("输入要修改的姓名>>>");
scanf("%s",name);
while(getchar()!=10);
sprintf(sql,"update stu set name=%s where id=%d",name,id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("update success\n");
goto END;
case '2':
printf("请输入要修改的成绩>>>");
scanf("%f",&score);
while(getchar()!=10);
sprintf(sql,"update stu set score=%f where id=%d",score,id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
goto END;
}
END:
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"line: %d sqlite3_open:%s\n",__LINE__,errmsg);
return -1;
}
printf("update success\n");
}
结果