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 ...
随机推荐
- 创建带有IN类型参数的存储过程(四十八)
创建带有IN类型参数的存储过程 我们经常要从数据表中删除记录,一般情况我们删除记录都是根据id来删除的,比如我们通常要输入DELETE FROM 表名 WHERE 后面跟上我们的条件,因为我们要经常写 ...
- jython awt demo
jython awt demo : """\ Create a panel showing all of the colors defined in the pawt.c ...
- linux和Windows双系统让 Windows 把硬件时间当作 UTC
linux和Windows双系统让 Windows 把硬件时间当作 UTC Windows设置如下:开 始->运行->CMD,打开命令行程序(Vista则要以管理员方式打开命令行程序方可有 ...
- 【RHEL7/CentOS7网络配置】
目录 网卡配置文件 查网卡信息 测试网络是否正常 使用 nmtui 命令配置网络 使用 nm-connection-editor 工具配置网络 修改回6.x版本的网卡名 Rhel/CentOS网络配置 ...
- 在hive执行创建表的命令,遇到异常com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Specified key was too long; max key length is 767 bytes
今天在练习hive的操作时,在创建数据表时,遇到了异常 FAILED: Execution Error, return code 1 from org.apache.hadoop.hive.ql.ex ...
- javaweb:判断当前请求是否为移动设备访问
http://blog.csdn.net/educast/article/details/71157932
- Dynamics CRM2016 升级老版本号报“JavaScript Web 资源包括对 Microsoft Dynamics CRM 4.0 (2007) Web 服务终结点的引用”问题的解决的方法
今天在新的server上部署了CRM2016 on-premises,并将CRM2015的数据库拷贝过来准备附加后升级,但在升级过程中遇到了例如以下错误.向导检測到了我的JavaScript Web ...
- final使用方法
final的作用随着所修饰的类型而不同 1.final修饰类中的属性或者变量 不管属性是基本类型还是引用类型.final所起的作用都是变量里面存放的"值"不能变. 这个值,对 ...
- HBase 数据库检索性能优化策略
HBase 数据表介绍 HBase 数据库是一个基于分布式的.面向列的.主要用于非结构化数据存储用途的开源数据库.其设计思路来源于 Google 的非开源数据库"BigTable" ...
- vim 基础学习之查找
普通模式下 /->正向查找 n-向下查找 N-向上查找 ?->反向查找 N-向下查找 n-向上查找 <C-r><C-w> <C-r>-引用,例如引用寄存 ...