作业:数据库操作的增、删、改完成
(目前只能实现静态管理)
源代码:
#include <myhead.h>
void do_add(sqlite3 *ppDb)
{
char *errmsg = NULL;
char sql[128] = "insert into 'Worker' values(1001,'小张',15000);";
// "insert into 'Worker' values(1002,'小刘',9000);"
// "insert into 'Worker' values(1003,'小徐',21000);"
// "insert into 'Worker' values(1004,'小于',7800);"
if (sqlite3_exec(ppDb, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("exec error:%s\n", errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
return;
}
printf("Add success\n");
return;
}
void do_del(sqlite3 *ppDb)
{
char *errmsg = NULL;
char sql[128] = "delete from Worker where numb = 1001;";
if (sqlite3_exec(ppDb, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("exec error:%s\n", errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
return;
}
printf("Delete success\n");
}
void do_update(sqlite3 *ppDb)
{
char *errmsg = NULL;
char sql[128] = "update Worker set salary = 8000 where numb = 1001;";
if (sqlite3_exec(ppDb, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("exec error:%s\n", errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
return;
}
printf("Update success\n");
}
void do_search(sqlite3 *ppDb) {}
int main(int argc, const char *argv[])
{
sqlite3 *ppDb = NULL;
if (sqlite3_open("./my.db", &ppDb) != SQLITE_OK)
{
printf("sqlite3_open error:%s\n", sqlite3_errmsg(ppDb));
return -1;
}
printf("sqlite3_open success\n");
//创建数据表
char sql[128] = "create table if not exists Worker(numb int,name char,salary double)";
char *errmsg = NULL;
if (sqlite3_exec(ppDb, sql, NULL, NULL, &errmsg) != SQLITE_OK)
{
printf("exec error:%s\n", errmsg);
sqlite3_free(errmsg);
errmsg = NULL;
return -1;
}
printf("Worker table create success\n");
int menu = -1;
while (1)
{
system("clear");
printf("\t\t=======xx公司员工管理系统xx====\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("Enter mode:");
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_search(ppDb);
}
break;
case 0:
goto END;
default:
printf("输入错误,请重新输入\n");
}
printf("请输入任意键,按回车清屏\n");
while (getchar() != '\n')
;
}
END:
sqlite3_close(ppDb);
return 0;
}