嵌入式數據庫 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

 

 

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