#include <myhead.h>int do_add(sqlite3 *ppDb)
{int numb;char name[20];int age;int salary;printf("请输入要插入的信息:");scanf("%d %s %d %d", &numb, name, &age, &salary);char sql[128] = "";sprintf(sql, "INSERT INTO worker VALUES(%d,'%s',%d,%d)", numb, name, age, salary);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;}
}
int do_delete(sqlite3 *ppDb)
{char sql[128] = "DELETE FROM worker where name = 'xzh'";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;}
}
int do_update(sqlite3 *ppDb)
{char sql[128] = "UPDATE worker SET salary = 16000 where name = 'ghn'";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;}
}int callback(void *arg,int argc, char **argv, char **azColName)
{int i; for(i=0; i<argc; i++){ printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; }
int do_search(sqlite3 *ppDb)
{struct INFO msg;char sql[128] = "SELECT * FROM worker WHERE numb = 1001";char *errmsg = NULL;if (sqlite3_exec(ppDb, sql,callback,0,&errmsg) != SQLITE_OK){printf("exec error : %s\n", errmsg);sqlite3_free(errmsg);errmsg = NULL;return -1;}
}
int main(int argc, char const *argv[])
{// 1.定义数据库句柄指针sqlite3 *ppDb = NULL;// 2.打开数据库,如果数据库不存在,则创建数据库并打开数据库// 将句柄由参数2返回,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");/*// 创建数据库// 1.准备sql语句char sql[128] = "CREATE TABLE worker(numb int PRIMARY KEY, name char, age int, salary double)";// char *sql = "CREATE TABLE worker(numb int PRIMARY KEY, name char, age int, 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_delete(ppDb);}break;case 3:{do_update(ppDb);}break;case 4:{do_search(ppDb);}break;case 0:goto END;default:printf("您输入的功能有误,请重新输入!!!\n");}getchar();printf("请输入任意键,按回车清屏\n");while (getchar() != '\n');}END:// 关闭数据库sqlite3_close(ppDb);return 0;
}