代码实现:
#include <head.h>
int add_msg(sqlite3* ppdb)
{
int id;
char name[20];
int age;
double salary;
//输入要插入的工人信息
printf("请输入工号:");
scanf("%d",&id);
printf("请输入姓名:");
scanf("%s",name);
printf("请输入年龄:");
scanf("%d",&age);
printf("请输入工资:");
scanf("%lf",&salary);
getchar();
//准备sql语句
char sql[128]="insert into Worker values(id,name,age,salary)";
//将格式串转换为字符串
sprintf(sql,"insert into Worker values(%d,'%s',%d,%.2lf);",id,name,age,salary);
printf("sql=%s\n",sql);
//定义储存错误信息的指针
char* errmsg=NULL;
//调用执行sql语句的函数
sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg);
//释放
sqlite3_free(errmsg);
printf("insert success\n");
return 0;
}
int delete_msg(sqlite3* ppdb)
{
int id;
printf("请输入要删除的信息id号:");
scanf("%d",&id);
getchar();
//准备sql语句
char sql[128]="";
sprintf(sql,"delete from Worker where id=%d;",id);
printf("sql=%s\n",sql);
//定义储存错误信息的指针
char* errmsg=NULL;
//调用执行sql语句的函数
sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg);
//释放
sqlite3_free(errmsg);
printf("delete success\n");
return 0;
}
int update_msg(sqlite3* ppdb)
{
int old_id;
int new_id;
char name[128]="";
int age;
double salary;
printf("请输入要修改的信息id号:");
scanf("%d",&old_id);
printf("请输入修改的id号:");
scanf("%d",&new_id);
printf("请输入要修改的姓名:");
scanf("%s",name);
printf("请输入要修改的年龄:");
scanf("%d",&age);
printf("请输入要修改的薪资:");
scanf("%lf",&salary);
getchar();
//准备sql语句
char sql[128]="";
sprintf(sql,"update Worker set id=%d,name='%s',age=%d,salary=%.2lf where id=%d;",new_id,name,age,salary,old_id);
printf("sql=%s\n",sql);
//定义储存错误信息的指针
char* errmsg=NULL;
//调用执行sql语句的函数
sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg);
//释放
sqlite3_free(errmsg);
printf("update success\n");
return 0;
}
//定义回调函数
int callback(void* arg,int cols,char** col_text,char** col_name)
{
//打印表头
if(*((int*)arg)==0)
{
(*((int*)arg))++; //标识位自增
for(int i=0;i<cols;i++)
{
printf("%5s\t",*(col_name+i));
}
putchar(10);
}
//打印数据
for(int i=0;i<cols;i++)
{
printf("%5s\t",*(col_text+i));
}
putchar(10);
return 0;
}
int search_msg(sqlite3* ppdb)
{
//准备sql语句
char sql[]="select * from Worker;";
char* errmsg=NULL;
int arg=0; //标识位
sqlite3_exec(ppdb,sql,callback,&arg,&errmsg);
sqlite3_free(errmsg);
printf("查找成功\n");
return 0;
}
int main(int argc, const char *argv[])
{
//1.打开数据库
//定义句柄指针
sqlite3* ppdb=NULL;
if(sqlite3_open("my.db",&ppdb)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_open error,errcode:%d,errmsg:%s\n",sqlite3_errcode(ppdb),sqlite3_errmsg(ppdb));
return -1;
}
printf("sqlite3_open success\n");
//2.执行sql语句
//2.1准备sql语句
char sql[128]="create table if not exists Worker(id int primery key,name char,age int,salary double);";
//2.2定义接收错误信息的指针
char* errmsg=NULL;
if(sqlite3_exec(ppdb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)
{
fprintf(stderr,"sqlite3_exec error:%s\n",errmsg);
//释放errmsg的内存
sqlite3_free(errmsg);
return -1;
}
printf("create table success\n");
//增删改查操作
int menu;
while(1)
{
system("clear");
printf("=====员工信息管理=====\n");
printf("=====1> 增加记录 =====\n");
printf("=====2> 删除记录 =====\n");
printf("=====3> 修改记录 =====\n");
printf("=====4> 查找记录 =====\n");
printf("=====0> 退出 =====\n");
printf("请输入选项:");
scanf("%d",&menu);
getchar(); //吸收scanf留下的回车
switch(menu)
{
case 1:
{
//增加记录
add_msg(ppdb);
}
break;
case 2:
{
//删除记录
delete_msg(ppdb);
}
break;
case 3:
{
//修改记录
update_msg(ppdb);
}
break;
case 4:
{
//查找记录
search_msg(ppdb);
}
break;
case 0: goto END; //退出数据库操作
default:printf("输入功能有误,请重新输入!\n");
}
//阻塞等待用户输入回车
printf("输入任意字符结束>>>");
while(getchar() != 10);
}
END:
//3.关闭数据库
sqlite3_close(ppdb);
return 0;
}
效果: