题目:
题目一:实现数据库的,增,删,改
题目二:把单词列表导入数据库
代码:
代码一:
#include<stdio.h>
#include<sqlite3.h>
#include<stdlib.h>
void do_insert(void);
void do_delete(void);
void do_update(void);
sqlite3 *db = NULL;
int main(int argc, const char *argv[])
{
if(sqlite3_open("./sq.db",&db)!=0)
{
fprintf(stderr,"sqlite3_open failed:%d:%s\n",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,"sqlite3_exec failed:%d:%s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
}
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();
break;
case '2':
do_delete();
break;
case '3':
do_update();
break;
case '4':
//do_select();
break;
case '5':
goto END;
break;
default:
printf("输入错误,请重新输入\n");
}
printf("请输入任意字符清屏\n");
while(getchar()!=10);
}
END:
if(sqlite3_close(db)!=0)
{
fprintf(stderr,"%d:sqlite3_close failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("sqlite3_close success\n");
return 0;
}
void do_insert(void)
{
char sql[128]="";
char *errmsg=NULL;
char id;
char name[20];
char score[10];
printf("请输入id,name,score:");
scanf("%c %s %s",&id,name,score);
while(getchar()!=10);
int size = sprintf(sql, "insert into stu values (%c,'%s',%s);",id,name,score);
printf("%s\n",sql);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"%d:sqlite3_exec failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("插入成功\n");
}
void do_delete(void)
{
char sql[128]="";
char *errmsg=NULL;
char id;
printf("请输入id:");
scanf("%c",&id);
while(getchar()!=10);
int size = sprintf(sql, "delete from stu where id=%c;",id);
printf("%s\n",sql);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"%d:sqlite3_exec failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("删除成功\n");
}
void do_update(void)
{
char sql[128]="";
char *errmsg=NULL;
char id;
char change[20];
printf("请输入要修改的id和要修改的内容:");
scanf("%c %s",&id,change);
while(getchar()!=10);
int size = sprintf(sql, "update stu set %s where id=%c;",change,id);
printf("%s\n",sql);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"%d:sqlite3_exec failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("修改成功\n");
}
代码二:
#include<stdio.h>
#include<sqlite3.h>
#include<stdlib.h>
void do_insert(void);
void do_delete(void);
void do_update(void);
sqlite3 *db = NULL;
int main(int argc, const char *argv[])
{
if(sqlite3_open("./sq.db",&db)!=0)
{
fprintf(stderr,"sqlite3_open failed:%d:%s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
return -1;
}
printf("sqlite3_open success\n");
char sql[128]="create table if not exists dictionary (word char,meaning char);";
char *errmsg=NULL;
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"sqlite3_exec failed:%d:%s\n",sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("table dictionary 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();
break;
case '2':
do_delete();
break;
case '3':
do_update();
break;
case '4':
//do_select();
break;
case '5':
goto END;
break;
default:
printf("输入错误,请重新输入\n");
}
printf("请输入任意字符清屏\n");
while(getchar()!=10);
}
END:
if(sqlite3_close(db)!=0)
{
fprintf(stderr,"%d:sqlite3_close failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("sqlite3_close success\n");
return 0;
}
void do_insert(void)
{
char sql[128]="";
char *errmsg=NULL;
char word[30];
char meaning[30];
FILE *fd=fopen("./dict.txt","r");
int res;
while((res=fscanf(fd,"%s ",word))!=EOF)
{
i++;
fgets(meaning,sizeof(meaning),fd);
sprintf(sql, "insert into dictionary values (\"%s\",\"%s\");",word,meaning);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"%d:sqlite3_exec failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
}
fclose(fd);
printf("插入成功\n");
}
/*void do_delete(void)
{
char sql[128]="";
char *errmsg=NULL;
char id;
printf("请输入id:");
scanf("%c",&id);
while(getchar()!=10);
int size = sprintf(sql, "delete from stu where id=%c;",id);
printf("%s\n",sql);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"%d:sqlite3_exec failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("删除成功\n");
}
void do_update(void)
{
char sql[128]="";
char *errmsg=NULL;
char id;
char change[20];
printf("请输入要修改的id和要修改的内容:");
scanf("%c %s",&id,change);
while(getchar()!=10);
int size = sprintf(sql, "update stu set %s where id=%c;",change,id);
printf("%s\n",sql);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=0)
{
fprintf(stderr,"%d:sqlite3_exec failed:%d:%s\n",__LINE__,sqlite3_errcode(db),sqlite3_errmsg(db));
}
printf("修改成功\n");
}*/
运行结果:
结果一:
增:
![](https://i-blog.csdnimg.cn/blog_migrate/3411f7e8b23fc87610a411f09b05314f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/7a1a90da93b102e7fed455fefa794d8b.png)
删:
![](https://i-blog.csdnimg.cn/blog_migrate/011f8cb3ae851f9e52837c0f9e9b0be8.png)
改:
![](https://i-blog.csdnimg.cn/blog_migrate/cac7384455d2fd828270cce6f5826979.png)
结果二:
![](https://i-blog.csdnimg.cn/blog_migrate/a8b3fbaf988fddcdd8a7f220a8f95fd3.png)