网络编程 9.10

完成代码剩余功能

typedef struct {

    int gh;

    char name[20];

    double salary;

} Worker;

int do_insert(sqlite3 *ppDb) {

    Worker work;

    printf("请输入插入的工号: ");

    scanf("%d", &work.gh);

    printf("请输入插入的姓名: ");

    scanf("%s", work.name);

    printf("请输入插入的薪水: ");

    scanf("%lf", &work.salary);

    char *errmsg = NULL;

    char sqlite[256];

    snprintf(sqlite, sizeof(sqlite), "INSERT INTO Work (gh, name, salary) VALUES (%d, '%s', %f);", work.gh, work.name, work.salary);

   

    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    printf("添加工人信息成功\n");

    return 0;

}

int callback(void *arg, int n, char **msgtable, char **msgtext) {

    for (int i = 0; i < n; i++) {

        printf("%s\t", msgtable[i]);

    }

    printf("\n");

    return 0;

}

int do_research(sqlite3 *ppDb) {

    char key[20];

    printf("请输入你要查找的工人姓名: ");

    scanf("%s", key);

    char *errmsg = NULL;

    char sqlite[256];

    snprintf(sqlite, sizeof(sqlite), "SELECT * FROM Work WHERE name = '%s';", key);

    if (sqlite3_exec(ppDb, sqlite, callback, NULL, &errmsg) != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    return 0;

}

int do_delete(sqlite3 *ppDb) {

    int gh;

    printf("请输入要删除的工号: ");

    scanf("%d", &gh);

    char *errmsg = NULL;

    char sqlite[256];

    snprintf(sqlite, sizeof(sqlite), "DELETE FROM Work WHERE gh = %d;", gh);

    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    printf("删除工人信息成功\n");

    return 0;

}

int do_update(sqlite3 *ppDb) {

    int gh;

    Worker work;

    printf("请输入要修改的工号: ");

    scanf("%d", &gh);

    printf("请输入新的姓名: ");

    scanf("%s", work.name);

    printf("请输入新的薪水: ");

    scanf("%lf", &work.salary);

    char *errmsg = NULL;

    char sqlite[256];

    snprintf(sqlite, sizeof(sqlite), "UPDATE Work SET name = '%s', salary = %f WHERE gh = %d;", work.name, work.salary, gh);

    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    printf("修改工人信息成功\n");

    return 0;

}

int do_showall(sqlite3 *ppDb) {

    char *errmsg = NULL;

    char sqlite[100] = "SELECT * FROM Work;";

    if (sqlite3_exec(ppDb, sqlite, callback, NULL, &errmsg) != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    return 0;

}

void menu(sqlite3 *ppDb) {

    int ch;

    printf("\t\t\t**********工人信息管理系统***************\n");

    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\t0.退出\n");

    printf("请输入你的选择: ");

    scanf("%d", &ch);

    switch (ch) {

        case 1:

            do_insert(ppDb);

            break;

        case 2:

            do_delete(ppDb);

            break;

        case 3:

            do_update(ppDb);

            break;

        case 4:

            do_research(ppDb);

            break;

        case 5:

            do_showall(ppDb);

            break;

        case 0:

            exit(0);

        default:

            printf("输入有误,请重新输入\n");

    }

}

int main(int argc, const char *argv[]) {

    sqlite3 *ppDb;

    int k = sqlite3_open("./banzhang.db", &ppDb);

    if (k != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    printf("数据库打开成功\n");

    char *errmsg = NULL;

    char sqlite[100] = "CREATE TABLE IF NOT EXISTS Work (gh INT, name CHAR, salary DOUBLE);";

    if (sqlite3_exec(ppDb, sqlite, NULL, NULL, &errmsg) != SQLITE_OK) {

        printf("错误码:%d 错误信息:%s\n", sqlite3_errcode(ppDb), sqlite3_errmsg(ppDb));

        return -1;

    }

    while (1) {

        menu(ppDb);

    }

    sqlite3_close(ppDb);

    return 0;

}

你可能感兴趣的:(数据库)