#include<myhead.h>
int main(int argc, const char *argv[])
{
int workid = 0;
char name[20] = "";
double salary = 0;
sqlite3 *ppDb = NULL;//数据库句柄指针
//打开数据库,如果数据库不存在,则创建数据库
//将数据库句柄由参数2返回
if((sqlite3_open("./my.db",&ppDb) != SQLITE_OK))
{
printf("sqlite3_open error,errcode = %d, errmsg = %s\n",\
sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));
return -1;
}
printf("sqlite3_open success\n");
//创建数据表
//1、准备sql语句
char sql[128] = "create table if not exists worker(numb int ,name char ,salary double)";
char *errmsg = NULL;//存放执行sql语句的错误信息
if(sqlite3_exec(ppDb, sql,NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("exec error :%s\n",errmsg);
sqlite3_free(errmsg);//防止内存泄露
errmsg = NULL;
return -1;
}
printf("员工信息表创建成功\n");
int menu = -1;
for(;;)
{
system("clear");
printf("\t\t======xxx公司员工管理系统======\n");
printf("\t\t========1、添加员工信息========\n");
printf("\t\t========2、删除员工信息========\n");
printf("\t\t========3、修改员工信息========\n");
printf("\t\t========4、查询员工信息========\n");
printf("\t\t========0、退出========\n");
printf("请输入操作码:");
scanf("%d",&menu);
getchar();
switch(menu)
{
case 1:
{
printf("请输入员工工号:");
scanf("%d",&workid);
printf("请输入员工名字:");
scanf("%s",name);
printf("请输入员工工资:");
scanf("%lf",&salary);
getchar();
sscanf(sql,"insert into worker values(%d %s %lf);",&workid,name,&salary);
if(sqlite3_exec(ppDb, sql,NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("exec error :%s\n",errmsg);
sqlite3_free(errmsg);//防止内存泄露
errmsg = NULL;
break;
}
printf("员工信息录入成功\n");
}break;
case 2:
{ printf("请输入要删除员工工号:");
scanf("%d",&workid);
getchar();
sscanf(sql,"update worker set where numb=%d;",&workid);
if(sqlite3_exec(ppDb, sql,NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("id不存在\n");
printf("exec error :%s\n",errmsg);
sqlite3_free(errmsg);//防止内存泄露
errmsg = NULL;
break;
}
printf("员工信息删除成功\n");
}break;
case 3:
{
printf("请输入要修改员工名字:");
scanf("%s",name);
printf("请输入员工工号:");
scanf("%d",&workid);
printf("请输入员工工资:");
scanf("%lf",&salary);
getchar();
sscanf(sql,"delete from worker numb=%d,salary=%lf where name=%s;",&workid,&salary,name);
if(sqlite3_exec(ppDb, sql,NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("名字不存在\n");
printf("exec error :%s\n",errmsg);
sqlite3_free(errmsg);//防止内存泄露
errmsg = NULL;
break;
}
printf("员工信息修改成功\n");
}break;
case 4:
{
char sql1[50] = "select * from worker";
char **result = NULL;
int hang = 1;
int lie = 3;
if(sqlite3_get_table(ppDb, sql1,&result,&hang, &lie , &errmsg) != SQLITE_OK)
{
printf("exec error :%s\n",errmsg);
sqlite3_free(errmsg);//防止内存泄露
errmsg = NULL;
break;
}
char *readresult = NULL;
&readresult = *result;
printf("%s\n",*readresult);
sqlite3_free(readresult);
readresult = NULL;
}break;
case 0:
goto END;
default:
printf("您输入的功能有误,请重新输入!!!\n");
}
printf("请输入任意键,按回车清屏\n");
while(getchar() != '\n');
}
END:
sqlite3_close(ppDb);
return 0;
}