#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;
}