使用数据库进行——增删改
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;
}
该代码示例展示了如何在C程序中使用SQLite3库进行数据库的基本操作,包括创建数据库、创建表格以及执行插入、删除和更新数据的函数。用户通过菜单选择不同的操作,程序会执行相应的SQL命令。
1267

被折叠的 条评论
为什么被折叠?



