- // 创建数据库, 插入表, 生效
- // 创建数据库, 插入表, 生效
- void create_database()
- {
- // 数据库指针
- sqlite3 *db=0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断是否打开
- if (res!=SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- // 创建数据库语句
- char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
- char * error;
- // 执行数据库语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- // 判断表格是否创建成功
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
创建数据库并实现绑定插入数据
- void create_table_by_bind()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语句, 创建数据库库
- char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
- char * error;
- // 执行 sql 语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 状态指针
- sqlite3_stmt *stmt;
- // 插入
- sqlite3_prepare_v2(db, "insert into hello (id,name)values(?,?)", -1, &stmt, 0);
- // 插入数据
- for (int i = 0; i < 100; i++)
- {
- char str[256] = { 0 };
- sprintf(str, "xiaowang%d", i);
- //i 与第一个参数绑定
- sqlite3_bind_int(stmt, 1, i);
- //str 与第二个参数绑定
- sqlite3_bind_text(stmt, 2, str, strlen(str), NULL);
- // 使状态生效
- sqlite3_step(stmt);
- // 插入后重置
- sqlite3_reset(stmt);
- }
- // 最终生效
- sqlite3_finalize(stmt);
- // 忽略错误
- sqlite3_free(error);
- sqlite3_close(db);
- }
- system("pause");
- }
插入一条数据
- // 插入一条数据
- void insert()
- {
- sqlite3 *db = 0;// 数据库指针
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断数据库是否打开成功
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- // 数据库语句
- char *sqlcreatetable = "insert into hello(id,name)values(1,xiaowang)";
- char * error;
- // 执行 sql 语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- // 判断语句是否执行成功
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
插入多条
- // 插入多条
- void insert_mul()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断数据库是否打开
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- char * error;
- for (int i = 2; i < 100; i++)
- {
- char strsql[256] = { 0 };
- // 格式化执行语句
- sprintf(strsql, "insert into hello(id,name)values(%d,xiaowang%d)", i, i);
- // 执行 sql 语句
- int res = sqlite3_exec(db, strsql, NULL, NULL, &error);
- // 判断 sql 语句是否执行成功
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
回调函数用于 sql 语句 select 使用
- // 回调函数用于 sql 语句 select 使用
- int showall(void *params, int n_column, char **column_value, char **column_name)
- {
- // 输出有多少列
- printf("n_column=%d\n", n_column);
- for (int i = 0; i < n_column;i++)
- {
- // 输出每一列
- printf("\t%s", column_value[i]);
- }
- printf("\n");
- return 0;
- }
遍历所有的数据
- // 遍历所有的数据
- void findall()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断数据库是否打开
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 执行语句
- char *sqlcreatetable = "select * from hello";
- char * error;
- // 执行 sql 语句, 调用回调函数 showall
- int res = sqlite3_exec(db, sqlcreatetable,showall, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格查询失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
不用回调函数遍历所有数据
- void findall_direct()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断是否打开数据库
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语句
- char *sql = "select * from hello";
- //result 存放读取的数据, error 存放错误信息
- char ** result,*error;
- int nrow, ncolumn;
- // 获取数据
- int res = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &error);
- // 输出表有多少行多少列
- printf("row=%d,column=%d\n", nrow, ncolumn);
- if (res==SQLITE_OK)
- {
- // 输出每一列的名字信息
- //result 前几个保存的是列的名字信息
- for (int j = 0; j < ncolumn; j++)
- {
- printf("%s\t", result[j]);
- }
- printf("\n");
- for (int i = 0; i < nrow;i++)// 遍历行
- {
- for (int j = 0; j < ncolumn; j++)
- {
- // 显示数据
- printf("%s\t", result[(i+1)*ncolumn+j]);
- }
- printf("\n");
- }
- }
- // 释放表
- sqlite3_free_table(result);
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
删除一条数据
- void delete_data()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语言
- //char *sqlcreatetable = "delete from hello where id<27";
- //char *sqlcreatetable = "delete from hello where name= xiaowang47
- char *sqlcreatetable = "delete from hello where name= haihua47 or id<45";
- char * error;
- // 执行 sql 语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
更新数据库
- void updata_data()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语句
- char *sqlcreatetable = "update hello set name=fangfang where id=50";
- char * error;
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
删除表
- void delete_table()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- // 删除所有表中的数据, 并删除表
- char *sqlcreatetable = "drop table if exists hello";
- char * error;
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- sqlite3_free(error);// 忽略错误
- sqlite3_close(db);
- }
- system("pause");
- }
完整代码
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- #include "sqlite3.h"
- // 项目属性, C/C++ SDL 选择否, 屏蔽安全生命周期检查
- // 创建数据库, 插入表, 生效
- void create_database()
- {
- // 数据库指针
- sqlite3 *db=0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断是否打开
- if (res!=SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- // 创建数据库语句
- char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
- char * error;
- // 执行数据库语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- // 判断表格是否创建成功
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 创建数据库并实现绑定插入数据
- void create_table_by_bind()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语句, 创建数据库库
- char *sqlcreatetable = "create table hello(id int primary key,name varchar(128))";
- char * error;
- // 执行 sql 语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 状态指针
- sqlite3_stmt *stmt;
- // 插入
- sqlite3_prepare_v2(db, "insert into hello (id,name)values(?,?)", -1, &stmt, 0);
- // 插入数据
- for (int i = 0; i < 100; i++)
- {
- char str[256] = { 0 };
- sprintf(str, "xiaowang%d", i);
- //i 与第一个参数绑定
- sqlite3_bind_int(stmt, 1, i);
- //str 与第二个参数绑定
- sqlite3_bind_text(stmt, 2, str, strlen(str), NULL);
- // 使状态生效
- sqlite3_step(stmt);
- // 插入后重置
- sqlite3_reset(stmt);
- }
- // 最终生效
- sqlite3_finalize(stmt);
- // 忽略错误
- sqlite3_free(error);
- sqlite3_close(db);
- }
- system("pause");
- }
- // 插入一条数据
- void insert()
- {
- sqlite3 *db = 0;// 数据库指针
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断数据库是否打开成功
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- // 数据库语句
- char *sqlcreatetable = "insert into hello(id,name)values(1,xiaowang)";
- char * error;
- // 执行 sql 语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- // 判断语句是否执行成功
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 插入多条
- void insert_mul()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断数据库是否打开
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- char * error;
- for (int i = 2; i < 100; i++)
- {
- char strsql[256] = { 0 };
- // 格式化执行语句
- sprintf(strsql, "insert into hello(id,name)values(%d,xiaowang%d)", i, i);
- // 执行 sql 语句
- int res = sqlite3_exec(db, strsql, NULL, NULL, &error);
- // 判断 sql 语句是否执行成功
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 回调函数用于 sql 语句 select 使用
- int showall(void *params, int n_column, char **column_value, char **column_name)
- {
- // 输出有多少列
- printf("n_column=%d\n", n_column);
- for (int i = 0; i < n_column;i++)
- {
- // 输出每一列
- printf("\t%s", column_value[i]);
- }
- printf("\n");
- return 0;
- }
- // 遍历所有的数据
- void findall()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断数据库是否打开
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 执行语句
- char *sqlcreatetable = "select * from hello";
- char * error;
- // 执行 sql 语句, 调用回调函数 showall
- int res = sqlite3_exec(db, sqlcreatetable,showall, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格查询失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 不用回调函数遍历所有数据
- void findall_direct()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- // 判断是否打开数据库
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语句
- char *sql = "select * from hello";
- //result 存放读取的数据, error 存放错误信息
- char ** result,*error;
- int nrow, ncolumn;
- // 获取数据
- int res = sqlite3_get_table(db, sql, &result, &nrow, &ncolumn, &error);
- // 输出表有多少行多少列
- printf("row=%d,column=%d\n", nrow, ncolumn);
- if (res==SQLITE_OK)
- {
- // 输出每一列的名字信息
- //result 前几个保存的是列的名字信息
- for (int j = 0; j < ncolumn; j++)
- {
- printf("%s\t", result[j]);
- }
- printf("\n");
- for (int i = 0; i < nrow;i++)// 遍历行
- {
- for (int j = 0; j < ncolumn; j++)
- {
- // 显示数据
- printf("%s\t", result[(i+1)*ncolumn+j]);
- }
- printf("\n");
- }
- }
- // 释放表
- sqlite3_free_table(result);
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 删除一条数据
- void delete_data()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语言
- //char *sqlcreatetable = "delete from hello where id<27";
- //char *sqlcreatetable = "delete from hello where name= xiaowang47
- char *sqlcreatetable = "delete from hello where name= haihua47 or id<45";
- char * error;
- // 执行 sql 语句
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 更新数据库
- void updata_data()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- //sql 语句
- char *sqlcreatetable = "update hello set name=fangfang where id=50";
- char * error;
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- // 忽略错误
- sqlite3_free(error);
- // 关闭数据库
- sqlite3_close(db);
- }
- system("pause");
- }
- // 删除表
- void delete_table()
- {
- // 数据库指针
- sqlite3 *db = 0;
- // 打开数据数据库, 初始化指针
- int res = sqlite3_open("1.db", &db);
- if (res != SQLITE_OK)
- {
- printf("数据库无法打开");
- getchar();
- return;
- }
- else
- {
- printf("数据库成功");
- // 删除所有表中的数据, 并删除表
- char *sqlcreatetable = "drop table if exists hello";
- char * error;
- int res = sqlite3_exec(db, sqlcreatetable, NULL, NULL, &error);
- if (res != SQLITE_OK)
- {
- printf("表格插入失败");
- getchar();
- return;
- }
- sqlite3_free(error);// 忽略错误
- sqlite3_close(db);
- }
- system("pause");
- }
来源: http://www.bubuko.com/infodetail-2514725.html