ubuntu@ubuntu:~/wlbc/day7$ cat sqlite01.c
int increas(sqlite3 *db,char *errmsg)
char aa[128]="INSERT INTO stu VALUES";
printf("输入要增加的数据 ID,NAME,SCORE\n");
scanf("%s %s %s",id,name,scoer);
sprintf(sql,"%s(%s,'%s',%s)",aa,id,name,scoer);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
int del_sqlite(sqlite3* db,char* errmsg)
printf("请输入你要删除你的数据信息\n");
scanf("%s %s %s",id,name,scoer);
char qq[128]="delete from stu where id";
sprintf(sql,"%s=%s",qq,id);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
int revise_sql(sqlite3* db,char* errmsg)
printf("请输入你要修改的数据的信息\n");
scanf("%s %s %s",id,name,scoer);
printf("如果要修改name,请输入要修改的name如果不是请输入NO\n");
printf("如果要修改scoer,请输入要修改的scoer如果不是请输入NO\n");
char qq[128]="update stu set";
sprintf(sql,"%s name='%s'where id=%s",qq,name1,id);
sprintf(sql2,"%s score=%s where id=%s",qq,scoer1,id);
if(strcmp(name1,"NO")!=0)
if(strcmp(scoer1,"NO")!=0)
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
if(sqlite3_exec(db,sql2,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
if(strcmp(scoer1,"NO")!=0)
if(sqlite3_exec(db,sql2,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"sqlite3_exec:%s\n",errmsg);
int main(int argc, const char *argv[])
if(sqlite3_open("./my.db",&db)!= SQLITE_OK)
fprintf(stderr,"line:%d sqlite3_open:%s\n",__LINE__,sqlite3_errmsg(db));
fprintf(stderr,"line:%d sqlite3_open:%d\n",__LINE__,sqlite3_errcode(db));
char sql[128]="create table if not exists stu (id int,name char,score float);";
if(sqlite3_exec(db,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
fprintf(stderr,"line:%d sqlite3_exec:%s\n",__LINE__,errmsg);
printf("------------------------------\n");
printf("---------1.添加---------------\n");
printf("---------2.删除---------------\n");
printf("---------3.改-----------------\n");
printf("------------------------------\n");
printf("----------5.退出--------------\n");
if(sqlite3_close(db)!=SQLITE_OK)
fprintf(stderr,"line:%d sqlite3_open: %s\n",__LINE__,sqlite3_errmsg(db));
fprintf(stderr,"line:%d sqlite3_open: %d\n",__LINE__,sqlite3_errcode(db));