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 ...
随机推荐
- vue.js原生组件化开发(一)——组件开发基础
前言 vue作为一个轻量级前端框架,其核心就是组件化开发.我们一般常用的是用脚手架vue-cli来进行开发和管理,一个个组件即为一个个vue页面,这种叫单文件组件.我们在引用组件之时只需将组件页面引入 ...
- 《linux 内核全然剖析》编译linux 0.12 内核 Ubuntu 64bits 环境
我×.. . 最终好了,大概3 4个小时吧...各种毛刺问题.终究还是闯过来了.. .. ubuntu2@ubuntu:~/Downloads/linux-0.00-050613/linux-0.00 ...
- js正則表達式--验证表单
检測手机号码:/0? (13|14|15|18)[0-9]{9}/ 检測username:(数字,英文,汉字.下划线.中横线):/^[A-Za-z0-9_\-\u4e00-\u9fa5]+$/ pas ...
- jquery08
<!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...
- u盘安装14.04ubuntu系统
最近在安装ubuntu 14.04系统,总结了下安装的方法,记录如下 1.下载ubuntu 14.04 iso文件,下载地址 http://www.ubuntu.com/download/deskto ...
- BZOJ 1355 KMP中next数组的应用
思路: 我们知道 next[i]是失配的i下一步要去哪儿 next[n]就是失配的n要去哪儿 n-next[n]就是答案(即最短周期)啦 //By SiriusRen #include <cst ...
- Redhat Linux下如何使用KVM虚拟机(视频)
KVM(kernel-basedVirtualMachine)是一个开源的系统虚拟化模块,自Linux2.6.20之后集成在Linux的各个主要发行版本中.它使用Linux自身的调度器进行管理,所以相 ...
- Standalone 集群部署
Spark中调度其实是分为两个层级的,即集群层级的资源分配和任务调度,以及任务层级的任务管理.其中集群层级调度是可配置的,Spark目前提供了Local,Standalone,YARN,Mesos.任 ...
- Socket实例之客户端向服务端数据库上传文件UI版
http://blog.csdn.net/su20145104009/article/details/52843735 首先实现分析: 1用户注册 客户单选择‘用户注册’,提示要输入用户名,密码,确认 ...
- Javascript和jquery事件--事件冒泡和事件捕获
jQuery 是一个 JavaScript 库,jQuery 极大地简化了 JavaScript 编程,在有关jq的描述中,jq是兼容现有的主流浏览器,比如谷歌.火狐,safari等(当然是指较新的版 ...