嵌入式数据库 sqlite3 移植及入门知识

sqlite3是嵌入式设备使用的开源数据库,具有占内存小、使用方便等优点。

sqlite3库可在其官网上直接下载,

sqlite3移植:

要使用sqlite3,首先得把源码库移植到你自己的设备上,其移植过程比较简单,步骤如下:

1、解压源码库压缩包:参考命令: tar -jzf sqlite-autoconf-3260000.tar.gz

2、进入解压出来的文件夹后,输入 ./configure 得到Makefile

如果要指定配置的路径,可加后缀 --prefix=/home/win/sqlite3

如果要指定交叉工具链,可加后缀 --host=arm-linux

所以,如果要得到用于嵌入式设备的sqlite3,应输入命令:

./configure --prefix=/home/win/sqlite3 --host=arm-linux

3、编译 make

4、make install

完成上述步骤,即可成功移植sqlite3,。

sqlite3使用:

sqlite3支持的常用数据类型:

int    整形        float   浮点型        char []   字符数组       text[]   字符串

其中char[] 和text 类型可看做是等价的,其余的类型,读者可自行百度一下。

sqlite3在Linux中的常用命令:(需要在sqlite3文件夹下输入,注意命令是否有小数点)

./sqlite3 first.db——创建、打开数据库文件

进入数据库文件后,命令行会打印如下信息,表示进入成功:

.exit/.quit——退出数据库文件(命令行)

.tables ——查看当前数据库文件中的所有表格名字

.dump ——查看之前输入的命令

操作数据库的命令(增删查改等):

创建表格:create table 表名(字段名1 类型,字段名2 类型 . . . . . .);

create table student(name text,sex char[],age int,score float);

其中字段名可以使用修饰符限制其取值,如:

unique——唯一性        not null——不为空 

create table student(name text not null,sex char[],age int,score float);        

创表时还可以使用if not exists判断表是否已存在,如果存在,则会创建失败:

create table if not exists student(name text,sex char[],age int,score float);

为某些字段加条件:

create table student(name text,score float,check(score>=0 and score<=100));

插入数据:insert into 表名 values(字段1,字段2 . . . );如:

insert into student values("zhangfei","man",22,99.6);

注意:插入时字段不允许比表格多,也不允许比表格少。

删除数据:delete from 表名 where 条件;

delete from student where score=88;

查询数据:

全表查询:select * from表名;

select * from student;

查询特定字段:select 字段名 from 表名;

select name,age from student;

条件查询:select * from 表名 where 条件;(条件可以多个,用and(并且)、or(或者)连接)

select * from student where sex="man";

select * from student where sex="man" and score>80;

升降序查询:asc——升序,desc——降序

select  * from student order by age asc;

select  * from student order by age desc;

模糊查询:

select  * from student where name like "%fei";

select  * from student where name like "%fei%";

修改数据:update 表名 set 字段= 条件;

update student set score=90 where name="zhangfei";

update student set score=90,age=21  where name="zhangfei";

删除表格:drop table 表名;

drop table student;

增加新字段:alter table 表名 add column 新字段名 类型;

alter table student add column height float;

sqlite还可以通过C语言进行开发,具体例程可参考以下博文:
https://blog.csdn.net/RwinR/article/details/84797988

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章