#include <myhead.h>
int do_add(sqlite3 *ppdb)
{
int numb;
char name[20];
double salary;
printf("请输入员工信息:numb name salary\n");
printf("请输入工号>>>");
scanf("%d",&numb);
printf("请输入员工姓名>>>");
// fgets(name,sizeof(name),stdin);
// name[strlen(name)-1]=0;
scanf("%s",name);
printf("请输入员工工资>>>");
scanf("%lf",&salary);
getchar();
char sql[128]="";
sprintf(sql,"insert into wker values(%d,\"%s\",%.2lf);",numb,name,salary);
char *errmsg=NULL;//存放执行sql语句中的错误信息
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("add error:%s\n",errmsg);
sqlite3_free(errmsg);
errmsg=NULL;
return -1;
}
printf("员工信息添加成功\n");
return 0;
}
//删除员工信息
int do_del(sqlite3 *ppdb)
{
printf("请输入要删除指定员工的工号>>>");
int numb;
scanf("%d",&numb);
char sql[128]="";
sprintf(sql,"delete from wker where numb=%d;",numb);
char *errmsg=NULL;//存放执行sql语句中的错误信息
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("del error:%s\n",errmsg);
sqlite3_free(errmsg);
errmsg=NULL;
return -1;
}
printf("员工信息删除成功\n");
return 0;
}
//修改员工信息
int do_update(sqlite3 *ppdb)
{
printf("请输入要修改员工对应的工号>>>");
int numb;
scanf("%d",&numb);
printf("请输入要修改员工的姓名>>>");
char name[20];
scanf("%s",name);
printf("请输入要修改员工的工资>>>");
double salary;
scanf("%lf",&salary);
getchar();
char sql[128]="";
sprintf(sql,"update wker set name=\'%s\',salary=%.2f where numb=%d;",name,salary,numb);
char *errmsg=NULL;//存放执行sql语句中的错误信息
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
printf("update error:%s\n",errmsg);
sqlite3_free(errmsg);
errmsg=NULL;
return -1;
}
printf("员工信息修改成功\n");
return 0;
}
int callback(void *arg,int cols,char** colum_text,char ** colum_name)
{
if((*(int*)arg)==0)
{
for(int i=0;i<cols;i++)//输出表头
{
printf("%8s",colum_name[i]);
}
printf("\n");
(*(int*)arg)=1;
}
//输出文本内容
for(int i=0;i<cols;i++)
{
printf("%8s",colum_text[i]);
}
printf("\n");
return 0;
}
//查询员工信息
int do_query(sqlite3 *ppdb)
{
int flag=0;
char sql[128]="select * from wker;";
char *errmsg=NULL;//存放执行sql语句中的错误信息
if(sqlite3_exec(ppdb,sql,callback,&flag,&errmsg)!=SQLITE_OK)
{
printf("query error:%s\n",errmsg);
sqlite3_free(errmsg);
errmsg=NULL;
return -1;
}
printf("查询员工信息成功\n");
return 0;
}
int main(int argc, const char *argv[])
{
//定义数据库操作的指针
sqlite3 *ppdb=NULL;
if(sqlite3_open("./my.db",&ppdb)!=SQLITE_OK)
{
printf("sqlite3_open error,errcode=%d\n",sqlite3_errcode(ppdb));
return -1;
}
printf("sqlite3_open success\n");
//创建数据表
//准备sql语句
char sql[128]="create table if not exists wker(numb int primary key,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:
{
do_add(ppdb);
}
break;
case 2:
{
do_del(ppdb);
}
break;
case 3:
{
do_update(ppdb);
}
break;
case 4:
{
do_query(ppdb);
}
break;
case 0:
goto END;
default:
printf("你输入的有误,请重新输入\n");
}
}
END:
//关闭数据库
sqlite3_close(ppdb);
return 0;
}
运行结果: