#include <myhead.h>
#include <sqlite3.h>

typedef struct
{
    int num;
    char name[20];
    double salary;
} Worker;
int callback(void *arg, int n, char **msgtext, char **msgtable)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf("%s\t", *(msgtable + i));
    }
    printf("\n");
    for (j = 0; j < n; j++)
    {
        printf("%s\t", *(msgtext + j));
    }
    printf("\n");
    printf("查找成功\n");
    return 0;
}

int do_delete(sqlite3 *ppDb)
{
    int key;
    printf("请输入要删除的工号:");
    scanf("%d", &key);
    char *errmsg = NULL; //储存错误信息
    char sqlite[100];
    snprintf(sqlite, sizeof(sqlite), "delete from Work where num==%d;", key);
    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != 0)
    {
        perror("sqlite3_exec");
        return -1;
    }
    printf("删除成功\n");
}
int do_insert(sqlite3 *ppDb)
{
    Worker work;
    printf("请输入要插入的工号:");
    scanf("%d", &work.num);
    printf("请输入要插入的姓名:");
    scanf("%s", work.name);
    printf("请输入要插入的工资:");
    scanf("%lf", &work.salary);
    char *errmsg = NULL; //储存错误信息
    char sqlite[100];
    snprintf(sqlite, sizeof(sqlite), "insert into Work values(%d,\"%s\",%lf);", work.num, work.name, work.salary);
    printf("添加成功\n");
    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != 0)
    {
        perror("sqlite3_exec");
        return -1;
    }
}

int do_update(sqlite3 *ppDb)
{
    Worker work;
    char key[20];
    printf("请输入要修改的姓名:");
    scanf("%s", work.name);
    printf("请输入新的工号:");
    scanf("%d", &work.num);
    printf("请输入新的工资:");
    scanf("%lf", &work.salary);

    char *errmsg = NULL; //储存错误信息
    char sqlite[100];
    snprintf(sqlite, sizeof(sqlite), "update Work set num=%d,salary=%lf where name=\"%s\";", work.num, work.salary, work.name);
    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != 0)
    {
        perror("sqlite3_exec");
        return -1;
    }
    printf("修改成功\n");
}

int do_reserch(sqlite3 *ppDb)
{
    char key[20];
    printf("请输入要查找的姓名:");
    scanf("%s", key);
    char *errmsg = NULL;
    char sqlite[100];
    snprintf(sqlite, sizeof(sqlite), "select * from Work where name==\"%s\";", key);
    if (sqlite3_exec(ppDb, sqlite, callback, NULL, &errmsg) != 0)
    {
        perror("sqlite3_exec");
        return -1;
    }
}

int show_all(void *arg, int n, char **msgtext, char **msgtable)
{
    int i, j;
    for (i = 0; i < n; i++)
    {
        printf("%s\t", *(msgtable + i));
    }
    printf("\n");
    for (j = 0; j < n; j++)
    {
        printf("%s\t", *(msgtext + j));
    }
    printf("\n");
    
    return 0;
}

int do_showall(sqlite3 *ppDb)
{
    char *errmsg = NULL;                                          //存储错误信息
    char sqlite[100] = "select * from Work";                      //输出所有工人信息
    if (sqlite3_exec(ppDb, sqlite, show_all, NULL, &errmsg) != 0) //输出所有工人信息
    {
        perror("sqlite3_exec");
        printf("错误码:%d错误信息:%s,出错行是:%d\n",
               sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb), __LINE__);
        printf("错误信息:%s\n", errmsg);
        return -1;
    }
    return 0;
}

void menu(sqlite3 *ppDb)
{
    int ch;
    printf("1.插入一个工人信息\n");
    printf("2.删除一个工人信息\n");
    printf("3.修改一个工人信息\n");
    printf("4.查找一个工人信息\n");
    printf("5.显示所有工人信息\n");
    printf("0.退出\n");
    printf("请输入选项:");
    scanf("%d", &ch);
    getchar();
    switch (ch)
    {
    case 1:
        do_insert(ppDb);
        break;

    case 2:
        do_delete(ppDb);
        break;

    case 3:
        do_update(ppDb);
        break;

    case 4:
        do_reserch(ppDb);
        break;

    case 5:
        do_showall(ppDb);
        break;

    case 0:
        exit(0);
    default:
        break;
    }
}

int main(int argc, char const *argv[])
{
    sqlite3 *ppDb;
    int k = sqlite3_open("./workers.db", &ppDb);
    if (k != SQLITE_OK)
    {
        perror("sqlite3_open");
        return -1;
    }
    printf("数据库打开成功\n");
    char *errmsg = NULL;                                                                    //储存错误信息
    char sqlite[100] = "create table if not exists Work(num int,name char,salary double);"; //创建一个表格
    if (sqlite3_exec(ppDb, sqlite, callback, NULL, &errmsg) != 0)
    {
        perror("sqlite3_exec");
        return -1;
    }
    while(1)
    {
        menu(ppDb);
    }

    sqlite3_close(ppDb);
    return 0;
}

点赞(0) 打赏

评论列表 共有 0 条评论

暂无评论

微信公众账号

微信扫一扫加关注

发表
评论
返回
顶部