当前位置:   article > 正文

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

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));

2. 插入数据

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

 

insert into student values('20200101', '张三', '广州','25648');
insert into student values('20200102', '李四', '广州','25645');

3. 查询数据

(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 条件;

4. 更新数据

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

 

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

5. 删除数据

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

delete  from 表名  where  条件;
 

delete from student where number='20200103';

6. alter添加字段

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

alter table student add column age int ;

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

7. 创建带约束的数据表

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

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

 sqlite3_close

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

  sqlite3_exec

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

   sqlite3_get_table

  1. // 执行zsql指向的sql语句,将结果集相关数据的地址保存在函数的参数中
  2. int sqlite3_get_table(
  3. sqlite3 *db, /* An open database */
  4. const char *zSql, /* SQL to be evaluated */
  5. char ***pazResult, /* Results of the query */
  6. int *pnRow, /* Number of result rows written here */
  7. int *pnColumn, /* Number of result columns written here */
  8. char **pzErrmsg /* Error msg written here */
  9. );
  10. db: 数据库的标识
  11. zsql: SQL语句,以分号结尾
  12. pazResult: 指针数组的地址,记录结果集数据。
  13. 内存布局:先依次存放各列的列名,然后是每一行各列的值。
  14. pnRow:行数的指针地址
  15. pnColumn: 列数的指针地址
  16. pzErrmsg: 错误信息指针的地址
  17. void sqlite3_free_table(char **result);
  18. // 释放动态分配的收集结果的sqlite3_get_table参数pazResult

绑定参数都给定的sql位置

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

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

遍历查询结果的每一层

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

  1. map<string, ASF_FaceFeature> QueryFaceFeature()
  2. {
  3. ASF_FaceFeature asf_feature = {0, 0};
  4. map<string, ASF_FaceFeature> map;
  5. sqlite3_stmt *stmt;
  6. char *sql = "select name, feature_size, face_feature from face_data_table";
  7. int ret = sqlite3_prepare(db, sql, strlen(sql), &stmt, 0);
  8. int id = 0, len = 0;
  9. char * name;
  10. int feature_size;
  11. if (ret == SQLITE_OK)
  12. {
  13. while (sqlite3_step(stmt) == SQLITE_ROW) // 遍历查询结果的每一行
  14. {
  15. name = (char *)sqlite3_column_text(stmt, 0); // 获取name
  16. printf("name = %s\n", name);
  17. feature_size = sqlite3_column_int(stmt, 1); // 获取feature_size
  18. printf("feature_size = %d\n", feature_size);
  19. asf_feature.feature = (MByte *)malloc(feature_size); // 分配内存以存储人脸特征数据
  20. const void *feature = sqlite3_column_blob(stmt, 2); // 获取face_feature 字段
  21. memset(asf_feature.feature, 0, feature_size); // 将内存初始化为0
  22. memcpy(asf_feature.feature, feature, feature_size); // 复制人脸特征数据到 asf_feature.feature
  23. asf_feature.featureSize = feature_size; // 设置asf_feature的大小
  24. string str(name); // 将c字符串转为c++字符串
  25. map.insert(pair<string, ASF_FaceFeature>(str, asf_feature)); // 将数据插入map中
  26. }
  27. }
  28. sqlite3_finalize(stmt);
  29. sqlite3_close(db);
  30. return map;
  31. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/717831
推荐阅读
  

闽ICP备14008679号