摘要:数据库是用来存储和管理数据的专用软件,使得管理数据更加安全,方便和高效。数据库对数据的管理的基本单位是表(table),在嵌入式linux中有时候它也需要用到数据库,听起来好难,其实就是几个函数,掌握了就好。
一、常见的数据库
大型数据库(大型机)Oracle(亿级),中型数据库(分布式超大型)mysql(百万级),轻型数据库(嵌入式设备)sqlite(万级),访问数据库使用SQL语句,适用于所有的数据库。
二、安装SQLite3
有C环境就可以调用sqlite
2.1直接用命令安装
sudoapt-getupdate sudoapt-getinstallsqlite3
2.2 直接编译源码
将源码拷贝到Ubuntu的非共享目录解压
解压命令:
tarzvxfsqlite-autoconf-3380500.tar.gz
配置
cdsqlite-snapshot-201708031550 ./configure--prefix=/home/gec/sqlite
编译
make
安装
makeinstall
三、SQLite的使用
新建数据库文件 sqlite3数据库文件的路径//打开/创建 //比如:sqlite3 first.db

3.1 基本操作命令
.exit/.quit--------退出数据库命令行 .help--------------帮助说明信息 .tables------------查看当前数据库中所有的表
3.2 数据库访问的SQL语句
基本语法:
所有的SQL语句都以分号(;)结束 不区分大小写
3.3 新建表格
createtable表名(字段名1字段类型1,字段名2字段类型2,字段名3字段类型3,...); 比如: //创建一个stutbl的表,表中有3个字段 //分别是整数类型的学号id,字符串类型的name和整数类型的age createtablezhiguoxin(idint,namechar[20],ageint); //不存在则创建 createtableifnotexistszhiguoxin(idint,namechar[20],ageint); //如果希望表中某个字段的内容不重复,可以用unique修饰该字段 createtableifnotexistszhiguoxin(idintunique,namechar[20],ageint);

3.4 删除表格
droptable表名; //droptablezhiguoxin;
3.5 往表格中插入数据
insertinto表名values(字段值1,字段值2,字段值3,....); //字段值如果是字符串,必须用''(单引号)括起来 比如: insertintozhiguoxinvalues(1001,'刘尧',18); insertintozhiguoxinvalues(1002,'聂衍文',19); insertintozhiguoxinvalues(1003,'杨佳晨',20); insertintozhiguoxinvalues(1004,'冯华阳',21);

完成插入之后,zhiguoxin 的表格内容如下:
id | name | age |
---|---|---|
1001 | 刘尧 | 18 |
1002 | 聂衍文 | 19 |
1003 | 杨佳晨 | 20 |
1004 | 冯华阳 | 21 |
3.6 查询表中的数据
//查询表中的所有数据
select*from表名; //select*fromzhiguoxin;

3.7 查看数据库
可以把first.db数据库文件拷贝至windows下,使用SQLite Developer打开即可看到。SQLite Developer下载地址
https://mydown.yesky.com/pcsoft/443425.html

3.8 按条件查找
1.使用where指定查询条件
select*fromzhiguoxinwhereid=1003;//查询id值为1003的条目 select*fromzhiguoxinwhereage>=19andage<21; select * from zhiguoxin where age>=19orage<21;

2.指定查询的字段
selectid,name,agefromzhiguoxin;//只查询id,name,age的字段

3.使用where+like实现模糊查询
select*fromzhiguoxinwherenamelike'刘%';//查找名字以刘开头的条目

4.使用order by实现查询结果按某个字段的值升序/降序输出
select*fromzhiguoxinorderbyagedesc;//按年龄降序排序 select*fromzhiguoxinorderbyidasc;//按id升序排序

3.9 删除表中的条目
deletefrom表名where条件;//删除所有符合条件的条目 比如: deletefromzhiguoxinwhereid=1001;

3.10 更新(修改)表中的条目
update表名set字段名1=字段值1,字段名2=字段值2...where条件;//修改符合条件的条目 比如: updatezhiguoxinsetage=100whereid=1002;

3.11 SQLite中字段类型
数字:
int-------整型 smallint----短整型 tinyint-----微型整数(0~255) bit---------0or1 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语言访问接口,在C语言的环境下可以直接使用,使用这些接口的代码需要 sqlite的源码编译进可执行程序 或者 编译时链接sqlite的库。
4.1 打开 sqlite3_open
intsqlite3_open( constchar*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
4.2 关闭 sqlite3_close
intsqlite3_close(sqlite3*pDb); //传入要关闭的数据库的句柄
4.3 编译方法
1.直接编译源码 gccsqlite3.csqlite_test.c-pthread-ldl-osqlite_test 2.链接sqlite3的动态库 gccsqlite_test.c-pthread-ldl-lsqlite3-L/home/gec/sqlite/lib-osqlite_test //如果运行时找不到sqlite3的库,可以将编译出来的库文件拷贝到/usr/lib目录下(cp-r)
4.4 执行SQL语句的接口 sqlite3_exec
intsqlite3_exec( sqlite3*pDb,/*打开的数据库的句柄*/ constchar*sql,/*要执行的SQL语句*/ int(*callback)(void*arg,intcol,char**str,char**name), /*回调函数,处理SQL语句执行返回的结果(查询),一条结果调用一次 arg-exec的第四个参数 col-本条结果的字段数 str-记录字段值的数组 name-记录字段名的数组 回调函数必须返回SQLITE_OK*/ void*arg,/*传递给回调函数的第一个参数*/ char**errmsg/*错误信息*/ ); //成功返回SQLITE_OK,否则执行失败
几个例子
//连接数据库 intConnection_Sqlite3DataBase() { rc=sqlite3_open("./face_database/face.db",&db); if(rc!=SQLITE_OK) { fprintf(stderr,"Can'topendatabase:%s ",sqlite3_errmsg(db)); sqlite3_close(db); exit(1); } else printf("Youhaveopenedasqlite3databasenamedbind.dbsuccessfully! Congratulation!Havefun! "); return0; } //将图片插入到数据库 voidinsert_face_data_toDataBase(constchar*name,MByte*face_feature,MInt32featureSize) { sqlite3_prepare(db,"insertintoface_data_table(name,face_feature,feature_size)values(?,?,?);",-1,&stmt,NULL); sqlite3_bind_text(stmt,1,name,strlen(name),NULL); sqlite3_bind_blob(stmt,2,face_feature,featureSize,NULL); sqlite3_bind_int(stmt,3,featureSize); sqlite3_step(stmt); }

-
嵌入式
+关注
关注
5082文章
19123浏览量
305110 -
数据库
+关注
关注
7文章
3799浏览量
64381 -
函数
+关注
关注
3文章
4331浏览量
62598
原文标题:手把手教你在嵌入式设备中使用SQLite3
文章出处:【微信号:pzh_mcu,微信公众号:痞子衡嵌入式】欢迎添加关注!文章转载请注明出处。
发布评论请先 登录
相关推荐
数据库复制的设计和管理
数据库教程之数据库的创建与管理详细资料免费下载

数据库有哪些常见的应用结构数据库应用结构的使用资料概述

数据库系统的常见用户
数据库教程之SQL Server数据库管理的详细资料说明

常见MySQL数据库管理工具的介绍

评论