DAY8-SQL和sqlite的C语言访问

120 阅读6分钟

Qt高级课程安排:

1.数据库

2.音视频

3.串口

image-20220729180444266


数据库

概念

数据库是用来存储和管理数据的专用软件,使得管理数据更加安全,方便和高效。数据库对数据的管理的基本单位是表(table)。

常见的数据库

大型数据库(大型机) ----------- Oracle(亿级)

中型数据库(分布式超大型) -------- mysql(百万级)

轻型数据库(嵌入式设备) -------- sqlite(万级)

访问数据库使用SQL语句,适用于所有的数据库。

安装sqlite

有C环境就可以调用sqite

直接用命令安装

sudo apt-get update
sudo apt-get install sqlite3

直接编译源码

将源码拷贝到Ubuntu的非共享目录解压

image-20220825155934297

解压命令:tar -xzvf sqlite-snapshot-201708031550.tar.gz

配置

cd sqlite-snapshot-201708031550
./configure --prefix=/home/gec/sqlite

编译

make              

安装

make install           

安装完成之后sqlite3的可执行文件就会出现在/home/gec/sqlite/bin中,如果要直接使用可以将改文件拷贝到/usr/bin目录下。

image-20220825155939537

sqlite的使用

新建数据库文件

sqlite3 数据库文件的路径   //打开/创建
//比如:sqlite3 first.db
//打开数据库进入命令行

image-20220825155948030

基本操作命令

.exit/.quit -------- 退出数据库命令行
.help -------------- 帮助说明信息
.tables ------------ 查看当前数据库中所有的表

数据库访问的SQL语句

基本语法:
    所有的SQL语句都以分号(;)结束
    不区分大小写

新建表格

create table 表名(字段名1 字段类型1,字段名2 字段类型2,字段名3 字段类型3,...);
比如:
//创建一个stutbl的表,表中有3个字段
//分别是整数类型的学号id,字符串类型的name和整数类型的age
create table stutbl(id int,name char[20],age int);
//不存在则创建
create table if not exists stutbl(id int,name char[20],age int);

//如果希望表中某个字段的内容不重复,可以用unique修饰该字段
create table if not exists stutbl(id int unique,name char[20],age int);

删除表格

drop table 表名;

往表格中插入数据

insert into 表名 values(字段值1,字段值2,字段值3,....);
//字段值如果是字符串,必须用''(单引号)括起来
比如:
    insert into stutbl values(1001,'张飞',23);
    insert into stutbl values(1002,'赵云',19);
    insert into stutbl values(1003,'刘备',31);

完成插入之后,stutbl的表格内容如下:

idnameage
1001张飞23
1002赵云19
1003刘备31

查询表中的数据

//查询表中的所有数据
select * from 表名;

image-20220825155959709

练习:

创建如下的表格,添加三个数据并查询出来

id(非重复 int)name(char)class(int)grade(int)age(int)score(float)
1001lily43978.5
create table if not exists testtbl(id int unique,name char[20],class int,grade int,age int,score float);
//unique=>唯一标签
INSERT INTO TESTTBL VALUES(1001,'lily',4,3,9,78.5);
INSERT INTO TESTTBL VALUES(1002,'黄国桐',6,7,12,88.5);
INSERT INTO TESTTBL VALUES(1003,'秦逍',3,5,23,91.0);
INSERT INTO TESTTBL VALUES(1004,'韦申姿',9,2,16,81.5);

按条件查找:

 1.使用where指定查询条件
        select * from testtbl where class=6;//查询class值为6的条目
        select * from testtbl where score>=90 or score<80;
    2.指定查询的字段
        select id,name,score from testtbl;//只查询id,name,score的字段 
    3.使用where+like实现模糊查询
        select * from testtbl where name like '黄%';//查找名字以黄开头的条目
    4.使用order by实现查询结果按某个字段的值升序/降序输出
        select * from testtbl order by score desc;//按分数降序排序     
       select * from testtbl order by grade asc;//按年级升序排序       

删除表中的条目

delete from 表名 where 条件;//删除所有符合条件的条目
比如:
    delete from testtbl where id=1001;

更新(修改)表中的条目

update 表名 set 字段名1=字段值1,字段名2=字段值2... where 条件;//修改符合条件的条目
比如:
    update testtbl set age=17,score=95.5 where id=1002;

sqlite中字段类型

数字:

int ------- 整型
smallint ---- 短整型
tinyint ----- 微型整数(0~255)
bit --------- 0 or 1

float ------ 单精度浮点型
real ------- 双精度浮点型

字符串:

char ---------- 非unicode定长字符串 < 8000
varchar ------- 非unicode变长字符串 < 8000
text ---------- 非unicode变长字符串 < 2^32-1

nchar ---------- unicode定长字符串 < 8000
nvarchar ------- unicode变长字符串 < 8000
ntext ---------- unicode变长字符串 < 2^32-1

sqlite的C语言访问接口

sqlite本身自带C语言访问接口,在C语言的环境下可以直接使用,使用这些接口的代码需要 sqlite的源码编译进可执行程序 或者 编译时链接sqlite的库。

打开 ----------- sqlite3_open

int sqlite3_open(
  const char *filename,   /* 数据库的文件路径 */
  sqlite3 **ppDb          /* 输出参数:传出代表打开数据库的句柄 */
);
//成功返回SQLITE_OK,否则打开失败char ---------- 非unicode定长字符串 < 8000
varchar ------- 非unicode变长字符串 < 8000
text ---------- 非unicode变长字符串 < 2^32-1

nchar ---------- unicode定长字符串 < 8000
nvarchar ------- unicode变长字符串 < 8000
ntext ---------- unicode变长字符串 < 2^32-1

关闭 ------------ sqlite3_close

int sqlite3_close(sqlite3 *pDb);
//传入要关闭的数据库的句柄

编译方法(必须链接pthread库和dl库):

1.直接编译源码
    gcc sqlite3.c sqlite_test.c -pthread -ldl -o sqlite_test
2.链接sqlite3的动态库
    gcc sqlite_test.c -pthread -ldl -lsqlite3 -L /home/gec/sqlite/lib -o sqlite_test  

//如果运行时找不到sqlite3的库,可以将编译出来的库文件拷贝到/usr/lib目录下(cp -r)        

image-20220825160014836

执行SQL语句的接口 ----------------- sqlite3_exec

int sqlite3_exec(
  sqlite3 *pDb,                              /* 打开的数据库的句柄 */
  const char *sql,                           /* 要执行的SQL语句 */
  int (*callback)(void *arg,int col,char **str,char **name),  
  /* 回调函数,处理SQL语句执行返回的结果(查询),一条结果调用一次 
      arg - exec的第四个参数
      col - 本条结果的字段数
      str - 记录字段值的数组
      name - 记录字段名的数组
     回调函数必须返回SQLITE_OK */
  void *arg,                                 /* 传递给回调函数的第一个参数 */
  char **errmsg                              /* 错误信息 */
);
//成功返回SQLITE_OK,否则执行失败

练习:

有C环境就可以调用sqite

实现一个C语言的访问sqlite的代码,通过用户输入的id去查询数据库中对应的条目并打印。

如何在Qt中访问数据库

直接将sqlite3的源码加入到Qt项目中,在Qt代码中直接调用sqlite的接口

作业:

完成sqlite的Qt程序的插入和查询(先打印)