SQLite简介
SQLite的源代码是C,其源代码完全开放,是一个轻量级嵌入式数据库。
SQLite有以下特性:
零配置,无需安装和管理配置
储存在单一磁盘文件中的一个完整的数据库
数据库文件可以在不同字节顺序的机器间自由共享
支持数据库大小至2TB
足够小,全部源码大致3万行C代码,250KB
比目前流行的大多数数据库对数据的操作要快
SQLite数据库采用了模块化设计,由8个独立的模块构成,这些独立模块又构成了三个主要的子系统,模块将复杂的查询过程分解为细小的工作进行处理
在线安装命令
sudo apt-get update
sudo apt-get install libreadline6-dev libreadline-dev
sudo apt-get install sqlite3
sudo apt-get install libsqlite3-dev
常用命令
// 打开数据库,不存在则创建
sqlite3 dbname.db
.help //帮助
.quit //退出
.exit //退出
.database //显示当前数据库情况
.tables //查看数据库中的表
.schema tablename //查看表tablename的结构
常用语句
// sqlite3的sql语句以;号结束 (创建、增删改查等操作)
// 创建一张表
create table stuinfo(id integer, name text, age integer, score float);
// 插入一条记录
insert into stuinfo values(1001, 'zhangsan', 18, 80);
insert into stuinfo (id, name, score) values(1002, 'lisi', 90);
// 查看数据库记录
select * from stuinfo;
select * from stuinfo where score = 80;
select * from stuinfo where score = 80 and name= 'zhangsan';
select * from stuinfo where score = 80 or name='wangwu';
select name,score from stuinfo; // 查询指定的字段
select * from stuinfo where score >= 85 and score < 90;
// 删除一条记录
delete from stuinfo where id=1003 and name='zhangsan';
// 更新一条记录
update stuinfo set age=20 where id=1003;
update stuinfo set age=30, score = 82 where id=1003;
// 删除一张表
drop table stuinfo;
// 增加一列
alter table stuinfo add column sex char;
// 删除一列
create table stu as select id, name, score from stuinfo;
drop table stuinfo;
alter table stu rename to stuinfo;
// 数据库设置主键
create table info(id integer primary key autoincrement, name vchar);
相关函数
int sqlite3_open(const char *filename, sqlite3 **ppDb);
/*********************************************************************************
* 描述:打开数据库
* 参数:
filename:数据库路径名
ppDb:代指打开的数据库(打开的数据库句柄)
* 返回值:成功返回 SQLITE_OK
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
int sqlite3_close(sqlite3 *ppDb);
/*********************************************************************************
* 描述:关闭数据库
* 参数:
ppDb:为刚才使用sqlite3_open打开的句柄
* 返回值:成功返回 SQLITE_OK
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
int sqlite3_exec(sqlite3* ppDb, const char *sql, sqlite_callback, void *data, char **errmsg);
/*********************************************************************************
* 描述:执行sql语句
* 参数:
ppDb:为刚才使用sqlite3_open得到的句柄
sql:sql语句
data:给回调函数传参,不使用可以置NULL
sqlite_callback:回调函数(获取结果)用于查询,不使用可以置NULL
errmsg:返回的错误信息
* 返回值:成功返回 SQLITE_OK (代表SQL语句成功执行了)
* 回调函数:每查询到一条记录,调用一次回调函数
int callback(void* argc, int num, char**value, char**name);
//第一个参数 argc:给回调函数的参数
//第二个参数 num:代表字段数目(有几列)
//第三个参数 value:代表查询到的记录的结果 value[0], value[1], value[2]...
//第四个参数 name:代表字段名 name[0], name[1]...
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
const char *sqlite3_errmsg(sqlite3 *ppDb);
/*********************************************************************************
* 描述:错误解析
* 参数:
ppDb:为刚才使用sqlite3_open得到的句柄+
* 返回值:成功返回的错误信息,失败返回NULL
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
int sqlite3_get_table(sqlite3 *ppDb, const char *sql, char ***resultp, int *nrow, int *ncolumn, char **errmsg);
/*********************************************************************************
* 描述:执行sql语句(一般用于查询)非回调方式
* 参数:
ppDb:为刚才使用sqlite3_open得到的句柄
sql:sql语句
resultp:查询到的结果
nrow:查询到的记录 有几条
ncolumn:查询到的记录 有几列
errmsg:返回的错误信息
* 返回值:成功返回 SQLITE_OK (代表SQL语句成功执行了)
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
void sqlite3_free_table(char **result);
/*********************************************************************************
* 描述:释放sqlite3_get_table中开辟的空间
* 参数:
result:sqlite3_get_table中result
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
void sqlite3_free(void *errmsg);
/*********************************************************************************
* 描述:释放错误信息开辟的空间
* 参数:
errmsg:错误信息
* 头文件:
#include <sqlite3.h>
**********************************************************************************/
拼接SQL语句
#include <stdio.h>
int main(void)
{
char *name = "jack";
char *pswd = "abc123";
char sql[256];
// 通过snprintf 拼凑sql语句
snprintf(sql, sizeof(sql), "insert into user values('%s','%s')", name, pswd);
printf("sql:%s\n", sql);
return 0;
}
回调函数查询
#include <sqlite3.h>
#include <stdio.h>
// 设计回调函数,用于获取查询到的记录
// 每查询到一条记录,调用一次回调函数
int callback(void *argc, int num, char **value, char **name)
{
char *ptr = (char *)argc;
printf("传递过来的参数:%s\n", ptr);
// printf("do callback num:%d\n", num); // num代表字段数目(有两列)
// printf("do callback name[0]:%s,name[1]:%s\n", name[0], name[1]); // name[0],name[1] 记录的是字段名
// printf("do callback value[0]:%s,value[1]:%s\n", value[0], value[1]); // value[0],value[1] 记录的是结果
// printf("%s:%s,", name[0], value[0]);
// printf("%s:%s\n", name[1], value[1]);
// printf("%s,%s\n", value[0], value[1]);
for (int i = 0; i < num; i++)
{
// printf("%s:%s,",name[i],value[i]);
printf("%s,", value[i]);
}
printf("\n");
return 0; // 成功
}
int main(void)
{
sqlite3 *db;
int ret;
ret = sqlite3_open("./my.db", &db); // 打开,不存在则创建
if (ret != SQLITE_OK)
{
printf("open sqlite3 falied:%s\n", sqlite3_errmsg(db));
return -1;
}
printf("open my.db success\n");
// 创建语句
// char sql[256] = "create table if not exists record (word char,time char)";
// 插入
// char sql[256] = "insert into record values ('happy','9:55')";
// 更新
// char sql[256] = "update record set time='16:14' where word='apple'";
// 删除一条记录
// char sql[256] = "delete from record where word='happy'";
// 查询
char sql[256] = "select * from record";
char *errmsg;
ret = sqlite3_exec(db, sql, callback, "test", &errmsg);
if (ret != SQLITE_OK)
{
printf("%s\n", errmsg);
sqlite3_free(errmsg); // 释放错误信息开辟的空间
sqlite3_close(db); // 关闭数据库
return -1;
}
sqlite3_close(db); // 关闭数据库
return 0;
}
非回调查询
#include <sqlite3.h>
#include <stdio.h>
// sqlite3_get_table
// 通过非回调方式 进行查询
int main(void)
{
// 打开数据库
sqlite3 *db;
int ret;
ret = sqlite3_open("./my.db", &db);
if (ret != SQLITE_OK)
{
printf("open failed:%s\n", sqlite3_errmsg(db));
return -1;
}
// 查询数据库
char sql[256] = "select * from user";
char **resultp;
int nrow;
int ncol;
char *errmsg;
ret = sqlite3_get_table(db, sql, &resultp, &nrow, &ncol, &errmsg);
if (ret != SQLITE_OK)
{
printf("get_table failed:%s\n", errmsg);
sqlite3_free_table(resultp); // 释放sqlite3_get_table中开辟的空间
sqlite3_free(errmsg); // 释放错误信息开辟的空间
sqlite3_close(db); // 关闭数据库
return -1;
}
printf("查询到的记录的条数:%d\n", nrow);
printf("查询到的记录的列数:%d\n", ncol);
#if 0
printf("%s ",resultp[0]);
printf("%s ",resultp[1]);
printf("%s ",resultp[2]);
printf("%s ",resultp[3]);
printf("%s ",resultp[4]);
printf("%s ",resultp[5]);
printf("%s ",resultp[6]);
printf("%s ",resultp[7]);
#endif
int index = ncol;
for (int i = 0; i < nrow; i++, index = index + 2)
{
printf("%s,%s\n", resultp[index], resultp[index + 1]);
}
// 释放sqlite3_get_table中开辟的空间
sqlite3_free_table(resultp);
// 关闭数据库
sqlite3_close(db);
return 0;
}