108.sqllite3(C语言数据库库)详解
- //创建数据库,插入表,生效
//创建数据库,插入表,生效
void create_database()
{
//数据库指针
sqlite3 *db=;
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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(?,?)", -, &stmt, );
//插入数据
for (int i = ; i < ; i++)
{
char str[] = { };
sprintf(str, "xiaowang%d", i);
//i与第一个参数绑定
sqlite3_bind_int(stmt, , i);
//str与第二个参数绑定
sqlite3_bind_text(stmt, , 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 = ;//数据库指针
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
int res = sqlite3_open("1.db", &db);
//判断数据库是否打开
if (res != SQLITE_OK)
{
printf("数据库无法打开");
getchar();
return;
}
else
{
printf("数据库成功");
char * error;
for (int i = ; i < ; i++)
{
char strsql[] = { };
//格式化执行语句
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 = ; i < n_column;i++)
{
//输出每一列
printf("\t%s", column_value[i]);
}
printf("\n");
return ; } - 遍历所有的数据
//遍历所有的数据
void findall()
{
//数据库指针
sqlite3 *db = ;
//打开数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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 = ; j < ncolumn; j++)
{
printf("%s\t", result[j]);
}
printf("\n");
for (int i = ; i < nrow;i++)//遍历行
{
for (int j = ; j < ncolumn; j++)
{
//显示数据
printf("%s\t", result[(i+)*ncolumn+j]);
}
printf("\n");
}
} //释放表
sqlite3_free_table(result);
//忽略错误
sqlite3_free(error);
//关闭数据库
sqlite3_close(db);
}
system("pause"); } - 删除一条数据
void delete_data()
{
//数据库指针
sqlite3 *db = ;
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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=;
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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(?,?)", -, &stmt, );
//插入数据
for (int i = ; i < ; i++)
{
char str[] = { };
sprintf(str, "xiaowang%d", i);
//i与第一个参数绑定
sqlite3_bind_int(stmt, , i);
//str与第二个参数绑定
sqlite3_bind_text(stmt, , 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 = ;//数据库指针
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
int res = sqlite3_open("1.db", &db);
//判断数据库是否打开
if (res != SQLITE_OK)
{
printf("数据库无法打开");
getchar();
return;
}
else
{
printf("数据库成功");
char * error;
for (int i = ; i < ; i++)
{
char strsql[] = { };
//格式化执行语句
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 = ; i < n_column;i++)
{
//输出每一列
printf("\t%s", column_value[i]);
}
printf("\n");
return ; } //遍历所有的数据
void findall()
{
//数据库指针
sqlite3 *db = ;
//打开数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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 = ; j < ncolumn; j++)
{
printf("%s\t", result[j]);
}
printf("\n");
for (int i = ; i < nrow;i++)//遍历行
{
for (int j = ; j < ncolumn; j++)
{
//显示数据
printf("%s\t", result[(i+)*ncolumn+j]);
}
printf("\n");
}
} //释放表
sqlite3_free_table(result);
//忽略错误
sqlite3_free(error);
//关闭数据库
sqlite3_close(db);
}
system("pause"); } //删除一条数据
void delete_data()
{
//数据库指针
sqlite3 *db = ;
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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 = ;
//打开数据数据库,初始化指针
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");
}
108.sqllite3(C语言数据库库)详解的更多相关文章
- c语言贪吃蛇详解3.让蛇动起来
c语言贪吃蛇详解3.让蛇动起来 前几天的实验室培训课后作业我布置了贪吃蛇,今天有时间就来写一下题解.我将分几步来教大家写一个贪吃蛇小游戏.由于大家c语言未学完,这个教程只涉及数组和函数等知识点. 上次 ...
- c语言贪吃蛇详解5.GameOver功能与显示成绩
c语言贪吃蛇详解5.GameOver功能与显示成绩 以前我们已经做出来了一个能吃东西变长的蛇.不过它好像不会死... 现在就来实现一下game over的功能吧. 写个函数判断蛇是否撞到自己或者撞到墙 ...
- c语言贪吃蛇详解4.食物的投放与蛇的变长
c语言贪吃蛇详解4.食物的投放与蛇的变长 前几天的实验室培训课后作业我布置了贪吃蛇,今天有时间就来写一下题解.我将分几步来教大家写一个贪吃蛇小游戏.由于大家c语言未学完,这个教程只涉及数组和函数等知识 ...
- JVM 运行时数据区详解
一.运行时数据区 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干个不同数据区域. 1.有一些是随虚拟机的启动而创建,随虚拟机的退出而销毁,所有的线程共享这些数据区. 2.第二种则 ...
- C语言memset函数详解
C语言memset函数详解 memset() 的作用:在一段内存块中填充某个给定的值,通常用于数组初始化与数组清零. 它是直接操作内存空间,mem即“内存”(memory)的意思.该函数的原型为: # ...
- 3dTiles 数据规范详解[1] 介绍
版权:转载请带原地址.https://www.cnblogs.com/onsummer/p/12799366.html @秋意正寒 Web中的三维 html5和webgl技术使得浏览器三维变成了可能. ...
- C语言中字符串详解
C语言中字符串详解 字符串时是C语言中非常重要的部分,我们从字符串的性质和字符串的创建.程序中字符串的输入输出和字符串的操作来对字符串进行详细的解析. 什么是字符串? C语言本身没有内置的字符串类型, ...
- ContentProvider数据访问详解
ContentProvider数据访问详解 Android官方指出的数据存储方式总共有五种:Shared Preferences.网络存储.文件存储.外储存储.SQLite,这些存储方式一般都只是在一 ...
- C语言内存对齐详解(2)
接上一篇:C语言内存对齐详解(1) VC对结构的存储的特殊处理确实提高CPU存储变量的速度,但是有时候也带来了一些麻烦,我们也屏蔽掉变量默认的对齐方式,自己可以设定变量的对齐方式.VC 中提供了#pr ...
随机推荐
- JavaScript Debug调试技巧
收藏于:https://blog.fundebug.com/2017/12/04/javascript-debugging-for-beginners/
- ZOJ 3435 Ideal Puzzle Bobble 莫比乌斯反演
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4119 依然是三维空间内求(1,1,1)~(a,b,c)能看到的整点数,平移一下 ...
- ES6学习基础
1.let和const 与var不同,新的变量声明方式带来了一些不一样的特性,其中最重要的两个特性就是提供了块级作用域与不再具备变量提升 { let a = 20; } console.log(a); ...
- 【Codeforces Round #460 (Div. 2) D】Substring
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果有环 ->直接输出-1 (拓扑排序如果存在某个点没有入过队列,说明有环->即入队的节点个数不等于n 否则. 说明可以 ...
- 【TC SRM 718 DIV 2 B】Reconstruct Graph
[Link]: [Description] 给你两个括号序列; 让你把这两个括号序列合并起来 (得按顺序合并) 使得组成的新的序列为合法序列; 即每个括号都能匹配; 问有多少种合并的方法; [Solu ...
- [Python] Execute a Python Script
Python scripts can be executed by passing the script name to the python command or created as execut ...
- 2.Maven之(二)Maven生命周期
转自:https://blog.csdn.net/u012152619/article/details/51473404 我们在开发项目的时候,不断地在编译.测试.打包.部署等过程,maven的生命周 ...
- 31.Intellij idea 的maven项目如何通过maven自动下载jar包
转自:https://blog.csdn.net/u012851114/article/details/81872981 maven项目自动加载jar包 所需工具如下: Intellij IDEA 1 ...
- 暑假集训-二分图,网络流,2-SAT
匈牙利算法DFS bool dfs(int u){ ; i <= n; i++){ if(a[u][i] && !visit[i]){ visit[i] = true; || d ...
- web知识—协议
web使用超文本传输协议(HTTP,HyperText Transfer Protocol)进行通信.http在1990年左右出现,现在有0.9/1.0/1.1三个版本.在早期的互联网中的一些协议只能 ...