Sqlite3入门和c/c++下使用

1. SQLite3基本介绍

1.1 数据库的数据类型

1.2 基本语法

  1. 创建数据表格

create table 表名(字段名 数据类型, 字段名 数据类型);

create table student(id int, name varchar(256), address text, QQ char(32));

  1. 插入数据

insert into 表名 values('字段数据','字段数据','字段数据','字段数据' );

insert into student values('20200101', '张三', '广州','25648');

insert into student values('20200102', '李四', '广州','25645');

  1. 查询数据

(1) 查询

select 字段名...字段名 from 表名;

字段名如果是多个可以用逗号隔开,如果是所有可以用星号*

select * from student ;

select name, qq from student;

(2) 条件查询

select 字段名...字段名 from 表名 where 条件;

select * from student where address='广州';

select * from student where address like '广%';
条件里面的where address='广州'; 等于号表示必须一样, 如果是模糊查询address like '广%';

如果是需要多个条件可以加and, 如果两个条件只需要成立一个加or即可

select 字段名...字段名 from 表名 where 条件 and 条件;

select 字段名...字段名 from 表名 where 条件 or 条件;

  1. 更新数据

update 表名 set 字段1=字段1值, 字段2=字段2值... where 条件表达式;

update student set qq='199999999999' where name='岳飞';

  1. 删除数据

delete from 表名;//删除整个表数据,不会删除表格

delete from 表名 where 条件;

delete from student where number='20200103';

  1. alter添加字段

alter table 表名 add column 字段 类型 (default '默认值');

alter table student add column age int ;

alter table student add column sex varchar(8) default '男' ;

  1. 创建带约束的数据表

PRIMARY KEY主键,NOT NULL不能为NULL,UNIQUE唯一,DEFAULT默认值,ID INTEGER PRIMARY KEY AUTOINCREMENT id自动增长

create table device (id integer primary key autoincrement,

name varchar(256) unique ,

status int not NULL default 0,

online int not NULL);

2. sqlite3 c/c++

sqliite3_open

复制代码
int sqlite3_open(  // 打开数据库
  const char *filename,   /* Database filename (UTF-8) */
  sqlite3 **ppDb          /* OUT: SQLite db handle */
);
filename: 数据库文件路径
ppDb: 指向sqlite3的句柄指针
返回值: 0 SQLITE_OK 成功 其他错误码(非零值)失败

sqlite3_close

复制代码
int sqlite3_close(sqlite3* pdb); // 关闭数据库文件
pdb: 关闭的sqlite数据库
返回值: 0 SQLITE_OK 成功 其他错误码失败

sqlite3_exec

复制代码
int sqlite3_exec( // 执行SQL操作
  sqlite3* pdb,                                  /* An open database */
  const char *sql,                           /* SQL to be evaluated */
  int (*callback)(void*,int,char**,char**),  /* Callback function */
  void *arg,                                    /* 1st argument to callback */
  char **errmsg                              /* Error msg written here */
);
pdb: 数据库句柄
sql: SQL语句
callback: 回调函数,在使用select语句的时候使用,其他忽略
arg: 传递给回调函数的第一个参数
errmsg: 错误信息指针的地址
返回值: 0 SQLITE_OK 成功 其他错误码
// 回调函数 每找到一条记录,自动执行一次回调函数
typedef int (*sqlite3_callback)(void* arg, int f_num, char** f_value, char** f_name); 
arg: 传递给回调函数的参数
f_num: 记录中包含的字段数目
f_value: 包含每个字段值的指针数组
f_name: 包含每个字段名称的指针数组
返回值:0 成功 -1 失败

sqlite3_get_table

复制代码
// 执行zsql指向的sql语句,将结果集相关数据的地址保存在函数的参数中
int sqlite3_get_table( 
  sqlite3 *db,          /* An open database */
  const char *zSql,     /* SQL to be evaluated */
  char ***pazResult,    /* Results of the query */
  int *pnRow,           /* Number of result rows written here */
  int *pnColumn,        /* Number of result columns written here */
  char **pzErrmsg       /* Error msg written here */
);
db: 数据库的标识
zsql: SQL语句,以分号结尾
pazResult: 指针数组的地址,记录结果集数据。
    内存布局:先依次存放各列的列名,然后是每一行各列的值。
pnRow:行数的指针地址
pnColumn: 列数的指针地址
pzErrmsg: 错误信息指针的地址

void sqlite3_free_table(char **result);
// 释放动态分配的收集结果的sqlite3_get_table参数pazResult

绑定参数都给定的sql位置

先准备sql语句,然后使用prepare函数,最后使用对应的bind函数绑定参数都对应的?号上,其中bind的第二个参数表示第几个问号。

复制代码
void insert_face_data_toDataBase(const char *name, MByte *face_feature, MInt32 featureSize)
{
    // 准备sqlite3的插入语句
    sqlite3_prepare(db, "insert into face_data_table(name,face_feature,feature_size) values (?,?,?);", -1, &stmt, NULL);
    // 绑定name到第一个占位符
    sqlite3_bind_text(stmt, 1, name, strlen(name), NULL);
    // 绑定face_feature 到第二个占位符
    sqlite3_bind_blob(stmt, 2, face_feature, featureSize, NULL);
    // 绑定faceSize 到第三个占位符
    sqlite3_bind_int(stmt, 3, featureSize);
    sqlite3_step(stmt);
}

遍历查询结果的每一层

查询函数然后准备结果,一行一行遍历参数。

复制代码
map<string, ASF_FaceFeature> QueryFaceFeature()
{
    ASF_FaceFeature asf_feature = {0, 0};
    map<string, ASF_FaceFeature> map;
    sqlite3_stmt *stmt;
    char *sql = "select name, feature_size, face_feature from face_data_table";
    int ret = sqlite3_prepare(db, sql, strlen(sql), &stmt, 0);
    int id = 0, len = 0;
    char * name;
    int feature_size;

    if (ret == SQLITE_OK)
    {
        while (sqlite3_step(stmt) == SQLITE_ROW) // 遍历查询结果的每一行
        {
            name = (char *)sqlite3_column_text(stmt, 0);  // 获取name
            printf("name = %s\n", name);
            feature_size = sqlite3_column_int(stmt, 1);   // 获取feature_size
            printf("feature_size = %d\n", feature_size);
            asf_feature.feature = (MByte *)malloc(feature_size);  // 分配内存以存储人脸特征数据
            const void *feature = sqlite3_column_blob(stmt, 2);    // 获取face_feature 字段  
            memset(asf_feature.feature, 0, feature_size);   // 将内存初始化为0
            memcpy(asf_feature.feature, feature, feature_size); // 复制人脸特征数据到 asf_feature.feature
            asf_feature.featureSize = feature_size;   // 设置asf_feature的大小
            string str(name);    // 将c字符串转为c++字符串
            map.insert(pair<string, ASF_FaceFeature>(str, asf_feature));  // 将数据插入map中
        }
    }
    
    sqlite3_finalize(stmt);
    sqlite3_close(db); 

    return map;
}
相关推荐
广州智造4 小时前
OptiStruct实例:3D实体转子分析
数据库·人工智能·算法·机器学习·数学建模·3d·性能优化
技术宝哥7 小时前
Redis(2):Redis + Lua为什么可以实现原子性
数据库·redis·lua
学地理的小胖砸8 小时前
【Python 操作 MySQL 数据库】
数据库·python·mysql
dddaidai1239 小时前
Redis解析
数据库·redis·缓存
数据库幼崽9 小时前
MySQL 8.0 OCP 1Z0-908 121-130题
数据库·mysql·ocp
Amctwd9 小时前
【SQL】如何在 SQL 中统计结构化字符串的特征频率
数据库·sql
betazhou10 小时前
基于Linux环境实现Oracle goldengate远程抽取MySQL同步数据到MySQL
linux·数据库·mysql·oracle·ogg
lyrhhhhhhhh10 小时前
Spring 框架 JDBC 模板技术详解
java·数据库·spring
喝醉的小喵11 小时前
【mysql】并发 Insert 的死锁问题 第二弹
数据库·后端·mysql·死锁
付出不多12 小时前
Linux——mysql主从复制与读写分离
数据库·mysql