网络编程day6-(超时检测、数据库)

使用数据库进行——增删改

main.c

#include <stdio.h>
#include <sqlite3.h>
#include <stdlib.h>

int do_insert(sqlite3 *db); // 增
int do_delete(sqlite3 *db); // 删
int do_update(sqlite3 *db); // 改

int main(int argc, const char *argv[])
{
    // 如果数据库不存在,则创建后打开
    // 如果存在则直接打开
    sqlite3 *db = NULL;
    if (sqlite3_open("./my.db", &db) != SQLITE_OK)
    {
        fprintf(stderr, "sqlite3_open :%s errcode:%d\n", sqlite3_errmsg(db), sqlite3_errcode(db));
        return -1;
    }
    printf("sqlite3_open success\n");

    // 创建一个表
    // 注意:C代码中编写的SQL语句与在数据库中编写的一直
    char sql[128] = "create table if not exists stu (id int,name char, score float)";
    char *errmsg = NULL;

    if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK)
    {
        fprintf(stderr, "line:%d sqlite3_exec:%s\n", __LINE__, errmsg);
        return -1;
    }
    printf("create table stu success\n");

    char buf[128] = "";
    char c = 0;
    while (1)
    {
        system("clear");
        printf("---------------------------------------------\n");
        printf("-------------------1. 增---------------------\n");
        printf("-------------------2. 删---------------------\n");
        printf("-------------------3. 改---------------------\n");
        printf("-------------------4. 查---------------------\n");
        printf("-------------------5. 退出-------------------\n");
        printf("---------------------------------------------\n");

        printf("请输入>>>");
        c = getchar();
        while (getchar() != 10)
            ;

        switch (c)
        {
        case '1':
            do_insert(db);
            break;
        case '2':
            do_delete(db);
            break;
        case '3':
            do_update(db);
            break;
        case '4':
            // do_select
            break;
        case '5':
            goto END;
        default:
            printf("输入错误,请重新输入\n");
        }

        printf("输入任意字符清屏>>>");
        while (getchar() != 10)
            ;
    }

END:
    // 关闭数据库
    if (sqlite3_close(db) != SQLITE_OK)
    {
        fprintf(stderr, "sqlite3_close :%s errcode:%d\n", sqlite3_errmsg(db), sqlite3_errcode(db));
        return -1;
    }
    return 0;
}

// 增
int do_insert(sqlite3 *db)
{
    // 存储sql插入指令
    char sql[128] = "";

    // 定义存储参数的变量“id name score”
    int id = 0;
    char name[20] = "";
    float score = 0;

    printf("请输入要插入的“id”  “姓名” “成绩>>>\n");
    printf("注意输入相邻参数时用空格隔开");
    scanf("%d %s %f", &id, name, &score);
    getchar();

    // 把sql指令存入数组
    sprintf(sql, "insert into stu values (%d, '%s', %f)", id, name, score);

    // 发送并执行sql语句
    if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK)
    {
        printf("执行SQL插入命令错误\n");
        return -1;
    }
    printf("插入数据成功\n");

    return 0;
}

// 删
int do_delete(sqlite3 *db)
{
    // 存储sql插入指令
    char sql[128] = "";

    // 定义要删除的哪一列的id的变量
    int id = 0;

    printf("请输入要删除的“id”>>>\n");
    scanf("%d", &id);
    getchar();

    // 把sql指令存入数组
    sprintf(sql, "delete from stu where id = %d", id);

    // 发送并执行sql语句
    if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK)
    {
        printf("执行SQL插入命令错误\n");
        return -1;
    }
    printf("插入数据成功\n");

    return 0;
}

int do_update(sqlite3 *db)
{
    // 存储sql插入指令
    char sql[128] = "";

    // 定义存储修改参数的变量“id name score newid”
    int id = 0;
    int newid = 0;
    char name[20] = "";
    float score = 0;

    printf("请输入要修改的数据\n“原始id”\t“姓名”\t“成绩\t“新id”\n");
    printf("注意输入相邻参数时用空格隔开");
    scanf("%d %s %f %d", &id, name, &score, &newid);
    getchar();

    // 把sql指令存入数组
    sprintf(sql, "update stu set score = %f,name = '%s',id = %d where id = %d", score, name, newid, id);

    // 发送并执行sql语句
    if (sqlite3_exec(db, sql, NULL, NULL, NULL) != SQLITE_OK)
    {
        printf("执行SQL插入命令错误\n");
        return -1;
    }
    printf("插入数据成功\n");
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值