一、数据库
sqlite官网:www.sqlite.org
1.1 数据库的安装
离线安装:
sudo dpkg -i sqlite3_3.22.0-1ubuntu0.4_amd64.deb //数据库软件
sudo dpkg -i libsqlite3-dev_3.22.0-1ubuntu0.4_amd64.deb //数据库的库函数
在线安装:
sudo apt-get install sqlite3 libsqlite3-dev
如何确定自己是否已经安装成功了?
在终端输入 sqlite3 如果能进入到下面的界面说明安装成功了
代码实现:
01test.c
gcc 01test.c -lsqlite3
编译时,需要链接sqlite3的库 -lsqlite3
#include <stdio.h>
#include <sqlite3.h>
#include <unistd.h>
#include <stdlib.h>
sqlite3 *pro_init()
{
sqlite3 *my_db = NULL;
int ret = 0;
char *errmsg = NULL;
if (SQLITE_OK != (ret = sqlite3_open("test.db", &my_db)))
{
printf("sqlite3_open error:errno = [%d],errstr = [%s]\n", ret, sqlite3_errmsg(my_db));
exit(-1);
}
printf("打开数据库成功...\n");
// 建表
// IF NO EXISTS表示如果不存在就新建 如果存在 就直接使用
char sqlbuff[256] = "CREATE TABLE IF NOT EXISTS student(id INT PRIMARY KEY,name TEXT,score INT)"; // 程序中使用sql语句结尾不必加分号
printf("%s\n", sqlbuff);
if (SQLITE_OK != (ret = sqlite3_exec(my_db, sqlbuff, NULL, NULL, &errmsg)))
{
printf("sqlite3_exec error:errno = %d,errstr = %s\n", ret, errmsg);
exit(-1);
}
printf("建表成功..\n");
sqlite3_free(errmsg); // 如果使用了第5个参数需要手动释放空间
return my_db;
}
void print_menu()
{
printf("--------------------------------------------\n");
printf("| 1.添加 2.查找 3.修改 4.删除 5.退出 |\n");
printf("--------------------------------------------\n");
printf("请输入您的选择:");
}
// 添加学员信息
void insert_student(sqlite3 *my_db)
{
int input_id = 0;
int input_score = 0;
char input_name[32] = {0};
int ret = 0;
printf("请输入学员信息(id(int) name(char) score(int)):");
scanf("%d%s%d", &input_id, input_name, &input_score);
// 组装sql语句
char sqlbuff[256] = {0};
sprintf(sqlbuff, "INSERT INTO student VALUES(%d,'%s',%d)", input_id, input_name, input_score);
printf("sqlbuff : [%s]\n", sqlbuff);
// 执行sql语句
if (SQLITE_OK != (ret = sqlite3_exec(my_db, sqlbuff, NULL, NULL, NULL)))
{
printf("sqlite3_exec error:errno = %d,errstr = %s\n", ret, sqlite3_errmsg(my_db));
exit(-1);
}
printf("学员信息添加成功...\n");
return;
}
// 查找学员信息 使用sqlite3_get_table实现
void search_student(sqlite3 *my_db)
{
int ret = 0;
char **result = NULL;
int row = 0;
int column = 0;
// 组装sql语句
char sqlbuff[256] = "SELECT *FROM student";
// 执行sql语句
if (SQLITE_OK != (ret = sqlite3_get_table(my_db, sqlbuff, &result, &row, &column, NULL)))
{
printf("sqlite3_get_table error: errno = [%d],errstr = [%s]\n", ret, sqlite3_errmsg(my_db));
exit(-1);
}
// 先打印字段名
int i = 0;
int j = 0;
for (int i = 0; i < column; i++)
{
printf("%16s", result[i]);
}
printf("\n");
int index = i;
// 再打印字段值
for (i = 0; i < row; i++)
{
for (j = 0; j < column; j++)
{
printf("%16s", result[(i + 1) * 3 + j]);
}
printf("\n");
}
printf("学员信息查询成功...\n");
return;
}
void modify_student(sqlite3 *my_db)
{
printf("请输入要修改的学员id:");
int input_id = 0;
scanf("%d", &input_id);
char input_name[32] = {0};
int input_score = 0;
printf("请输入新学员的信息(name(char) score(int)):");
scanf("%s%d", input_name, &input_score);
int ret = 0;
char sqlbuff[256] = {0};
sprintf(sqlbuff, "UPDATE student SET name='%s',score=%d WHERE id=%d", input_name, input_score, input_id);
// 执行sql语句
if (SQLITE_OK != (ret = sqlite3_exec(my_db, sqlbuff, NULL, NULL, NULL)))
{
printf("sqlite3_exec error:errno = %d,errmsg = %s\n", ret, sqlite3_errmsg(my_db));
exit(-1);
}
printf("修改学员信息成功...\n");
return;
}
void del_student(sqlite3 *my_db)
{
printf("请输入要删除的学员id:");
int input_id = 0;
scanf("%d", &input_id);
// 组装sql语句
char sqlbuff[256] = {0};
int ret = 0;
sprintf(sqlbuff, "DELETE FROM student where id=%d", input_id);
// 执行sql语句
if (SQLITE_OK != (ret = sqlite3_exec(my_db, sqlbuff, NULL, NULL, NULL)))
{
printf("sqlite3_exec error:errno = %d,errmsg = %s\n", ret, sqlite3_errmsg(my_db));
exit(-1);
}
printf("学员信息删除成功...\n");
return;
}
int main(int argc, char const *argv[])
{
printf("欢迎使用..\n");
sqlite3 *my_db = NULL;
my_db = pro_init();
int choose = 0;
while (1)
{
print_menu();
scanf("%d", &choose);
switch (choose)
{
case 1:
insert_student(my_db);
break;
case 2:
search_student(my_db);
break;
case 3:
modify_student(my_db);
break;
case 4:
del_student(my_db);
break;
}
if (5 == choose)
{
printf("退出成功..\n");
break;
}
}
sqlite3_close(my_db);
printf("欢迎下次使用...\n");
return 0;
}
效果图: