#include<myhead.h>
#include<sqlite3.h>
int out_callback(void *arg,int column_num, char **msgrow,char **msgcolumn)//输出查找的工人信息
{
int i = 0, j = 0;
while(i<column_num)
{
printf("%s\t" ,*(msgcolumn+i));
printf("%s\t",*(msgrow+i));
i++;
}
printf("\n");
return 0;
}
int do_showall(sqlite3 *ppDb)
{
char *errmsg = NULL;
char output_exec[100] = "select *from Work;";
if(sqlite3_exec(ppDb,output_exec,out_callback,NULL,&errmsg)!=0)//实现插入一个工人信息
{
perror("sqlite3_errmsg");
printf("错误码:%d错误信息:%s,出错行是:%d\n",
sqlite3_errcode(ppDb),errmsg,LINE);
return -1;
}
}
int do_delete(sqlite3*ppDb)
{
char key[20];
char *errmsg = NULL;
printf("输入你想要删除的名字:");
scanf("%s",key);
char delete_exce[100];
snprintf(delete_exce,sizeof(delete_exce),"delete from Work where name==\"%s\";",key);
if(sqlite3_exec(ppDb,delete_exce,NULL,NULL,&errmsg)!=0)
{
perror("sqlite3_exec");
printf("错误码:%d错误信息:%s,出错行是:%d\n",
sqlite3_errcode(ppDb),errmsg,LINE);
return -1;
}
printf("删除成功\n");
}
int do_change(sqlite3*ppDb)
{
char key[20];
char *errmsg = NULL;
int num;
printf("输入工号:");
scanf("%d",&num);
printf("输入你根据工号想要修改的名字:");
scanf("%s",key);
char change_exce[100];
snprintf(change_exce,sizeof(change_exce),"update Work set name==\"%s\" where gh==%d;",key,num);
if(sqlite3_exec(ppDb,change_exce,NULL,NULL,&errmsg)!=0)
{
perror("sqlite3_exec");
printf("错误码:%d错误信息:%s,出错行是:%d\n",
sqlite3_errcode(ppDb),errmsg,LINE);
return -1;
}
printf("删除成功\n");
}
void menu(sqlite3 * ppDb)
{
int ch;
printf("\t\t\t*******工人阶级为主导的社会主义国家**********\n");
printf("\t\t\t1、插入一个工人信息\n");
printf("\t\t\t2、删除一个工人信息\n");
printf("\t\t\t3、修改一个工人信息\n");
printf("\t\t\t4、查找一个工人信息\n");
printf("\t\t\t5、输出所有工人信息\n");
printf("\t\t\t0、退出\n");
printf("请输入你的选择:");
scanf("%d",&ch);
getchar();//吸收回车
switch(ch)
{
case 1:
do_insert(ppDb);
break;
case 2:
do_delete(ppDb);
break;
case 3:
do_change(ppDb);
break;
case 4:
do_reserch(ppDb);
break;
case 5:
do_showall(ppDb);
break;
case 0:
exit(0);
break;
default:
printf("error");
break;
}
}