工人管理系统
#include<myhead.h>
#include<sqlite3.h>
sqlite3 * creat_sqlite()//创建数据库并返回数据库句柄
{const char *p="./my.db";sqlite3 *ppDb;if(sqlite3_open(p,&ppDb)!=SQLITE_OK)//调用数据库提供的第三方库函数{printf("打开数据库失败\n");printf("%s\n",sqlite3_errmsg(ppDb));//错误信息printf("%d\n",sqlite3_errcode(ppDb));//错误码printf("错误行:%d\n",__LINE__);perror("sqlite3_open");}return ppDb;//返回数据库句柄
}
void insert_worker(sqlite3 *ppDb)//插入员工信息
{char sql[1024];char *errmsg;//存储错误信息的指针sprintf(sql,"%s","create table work(num int primary key,name char,age int,salary int);");if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK)//执行sqlite语句的函数{printf("%s\n",sqlite3_errmsg(ppDb));//错误信息printf("%d\n",sqlite3_errcode(ppDb));//错误码printf("错误行:%d\n",__LINE__);}printf("创建表格成功\n");
}
void add_worker(sqlite3 *ppDb)//添加工人信息
{int num,age,salary;char name[50];char sql[1024];char *errmsg;//存储错误信息的指针printf("请输入工人编号: ");scanf("%d",&num);printf("请输入工人姓名: ");scanf("%s",name);printf("请输入工人年龄: ");scanf("%d",&age);printf("请输入工人工资: ");scanf("%d",&salary);sprintf(sql,"INSERT INTO work(num, name, age, salary) VALUES (%d,'%s',%d,%d);",num,name,age,salary);if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK) {printf("%s\n",sqlite3_errmsg(ppDb)); // 错误信息printf("%d\n",sqlite3_errcode(ppDb)); // 错误码printf("错误行:%d\n",__LINE__);}else{printf("添加工人信息成功\n");}
}
void update_worker(sqlite3 *ppDb) //修改工人信息
{int num,age,salary;char name[50];char sql[1024];char *errmsg; // 存储错误信息的指针printf("请输入要修改的工人编号: ");scanf("%d",&num);printf("请输入新的工人姓名: ");scanf("%s",name);printf("请输入新的工人年龄: ");scanf("%d",&age);printf("请输入新的工人工资: ");scanf("%d",&salary);sprintf(sql,"UPDATE work SET name='%s',age=%d,salary=%d WHERE num=%d;",name,age,salary,num);if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK) {printf("%s\n",sqlite3_errmsg(ppDb)); // 错误信息printf("%d\n",sqlite3_errcode(ppDb)); // 错误码printf("错误行:%d\n",__LINE__);}else{printf("修改工人信息成功\n");}
}
void show_all_workers(sqlite3 *ppDb) //展示所有信息
{char *errmsg; // 存储错误信息的指针char **result;int nrow,ncol;int i,j;char *sql="SELECT * FROM work;";if(sqlite3_get_table(ppDb,sql,&result,&nrow,&ncol,&errmsg)!=SQLITE_OK){printf("%s\n",sqlite3_errmsg(ppDb)); // 错误信息printf("%d\n",sqlite3_errcode(ppDb)); // 错误码printf("错误行:%d\n", __LINE__);}else{for(i=0;i<=nrow;i++){for(j=0;j<ncol;j++){printf("%s\t",result[i*ncol+j]);}printf("\n");}sqlite3_free_table(result); // 释放查询结果内存}
}
void delete_worker(sqlite3 *ppDb) //删除指定工人信息
{int num;char sql[1024];char *errmsg; // 存储错误信息的指针printf("请输入要删除的工人编号: ");scanf("%d",&num);sprintf(sql,"DELETE FROM work WHERE num=%d;",num);if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK) {printf("%s\n",sqlite3_errmsg(ppDb)); // 错误信息printf("%d\n",sqlite3_errcode(ppDb)); // 错误码printf("错误行:%d\n",__LINE__);}else{printf("删除工人信息成功\n");}
}
void delete_all_workers(sqlite3 *ppDb)//删除整表
{char *errmsg; // 存储错误信息的指针char *sql="DROP TABLE work;";if(sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg)!=SQLITE_OK) {printf("%s\n",sqlite3_errmsg(ppDb)); // 错误信息printf("%d\n",sqlite3_errcode(ppDb)); // 错误码printf("错误行:%d\n",__LINE__);}else{printf("删除整表成功\n");}
}
void menu()
{int ch;sqlite3 *ppDb;//数据库句柄while(1){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\t6、删除整表\n");printf("\t\t\t0、退出\n");printf("请输入你的选择:");scanf("%d",&ch);getchar();switch(ch){case 1:ppDb=creat_sqlite();//创建数据库函数break;case 2:insert_worker(ppDb);add_worker(ppDb);//添加工人信息break;case 3:update_worker(ppDb);//修改工人信息break;case 4:show_all_workers(ppDb);//展示所有信息break;case 5:delete_worker(ppDb);//删除指定工人信息break;case 6:delete_all_workers(ppDb);//删除整表break;case 0:exit(0);break;}}
}
int main(int argc, const char *argv[])
{menu();//调用菜单函数return 0;
}