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 ...
随机推荐
- 学习思考:思考>努力
学.习.思.考 学习.思考,这2个词,4个字,其实代表了4个不同的动作:学.习.思.考. 学,就是从外部(他人)获得. 习,就是练习,行动. 思,就是从内部(自己)获得. 考,就是考量,检测. 因此, ...
- linux下支持托盘的邮件客户端Sylpheed
在网上搜索了很多客户端想支持系统托盘,发现一个很不错的邮件客户端Sylpheed.设置方式和foxmail很像,最为重要的是支持系统托盘,很方便,默认没有开启,简单设置下:配置->通用首选项-& ...
- Linux桌面词典 GoldenDict词典
GoldenDict 是一款不错的.与StarDict(星际译王)类似的词典软件.它使用 WebKit作为渲染核心,格式化.颜色.图像.链接等支持一应俱全:支持多种词典文件格式,包括Babylon的 ...
- HRBUST 1818 石子合并问题--直线版
石子合并问题--直线版 Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on HRBUST. Original ...
- 欢天喜地迎国庆,国产开源编程语言 RPP 1.87 公布!
更新例如以下: 1.支持超级宏 2.修复bug 下载地址: https://github.com/roundsheep/rpp 超级宏能够随意定义语法,制约你的仅仅有想象力: void main() ...
- hdu1078 FatMouse and Cheese(记忆化搜索)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:pid=1078" target="_blank">http://acm. ...
- Eclipse怎么导入外来项目
从File中点击------>import----->General------>然后按下面的图片显示
- html5中的容器标签和文本标签
html5中的容器标签和文本标签 html中的容器级标签和文本级标签,css中的块级元素和行内元素是我们常常拿来比较的四个名词(行内块级暂时先不考虑). 容器标签 容器级的标签可以简单的理解为能嵌套其 ...
- CentOS6.5下的Nagios安装配置详解(图文)
最近因为,科研需要,接触上了Nagios,这里,我将安装笔记做个详解.为自己后续需要和博友们学习! VMware workstation 11 的下载 VMWare Workstation 11的安装 ...
- VS Code 终端显示问题
一.打开编辑器的终端时候,然后弹出了系统自带的cmd窗口 解决办法: Win+R 输入cmd 打开windows cmd窗口,窗口顶部右键属性,然后取消勾选使用旧版控制台,然后重启编辑器就行了. 二. ...