摘要:資料庫是用來存儲和管理資料的專用軟體,使得管理資料更加安全,友善和高效。資料庫對資料的管理的基本機關是表(table),在嵌入式linux中有時候它也需要用到資料庫,聽起來好難,其實就是幾個函數,掌握了就好。
一、常見的資料庫
大型資料庫(大型機)Oracle(億級),中型資料庫(分布式超大型)mysql(百萬級),輕型資料庫(嵌入式裝置)sqlite(萬級),通路資料庫使用SQL語句,适用于所有的資料庫。
二、安裝SQLite3
有C環境就可以調用sqlite
2.1直接用指令安裝
sudo apt-get update
sudo apt-get install sqlite3
2.2 直接編譯源碼
将源碼拷貝到Ubuntu的非共享目錄解壓
解壓指令:
tar zvxf sqlite-autoconf-3380500.tar.gz
配置
cd sqlite-snapshot-201708031550
./configure --prefix=/home/gec/sqlite
編譯
make
安裝
make install
三、SQLite的使用
建立資料庫檔案
sqlite3 資料庫檔案的路徑 //打開/建立
//比如:sqlite3 first.db
3.1 基本操作指令
.exit/.quit -------- 退出資料庫指令行
.help -------------- 幫助說明資訊
.tables ------------ 檢視目前資料庫中所有的表
3.2 資料庫通路的SQL語句
基本文法:
所有的SQL語句都以分号(;)結束
不區分大小寫
3.3 建立表格
create table 表名(字段名1 字段類型1,字段名2 字段類型2,字段名3 字段類型3,...);
比如:
//建立一個stutbl的表,表中有3個字段
//分别是整數類型的學号id,字元串類型的name和整數類型的age
create table zhiguoxin(id int,name char[20],age int);
//不存在則建立
create table if not exists zhiguoxin(id int,name char[20],age int);
//如果希望表中某個字段的内容不重複,可以用unique修飾該字段
create table if not exists zhiguoxin(id int unique,name char[20],age int);
3.4 删除表格drop table 表名;
//drop table zhiguoxin;
3.5 往表格中插入資料insert into 表名 values(字段值1,字段值2,字段值3,....);
//字段值如果是字元串,必須用''(單引号)括起來
比如:
insert into zhiguoxin values(1001,'劉堯',18);
insert into zhiguoxin values(1002,'聶衍文',19);
insert into zhiguoxin values(1003,'楊佳晨',20);
insert into zhiguoxin values(1004,'馮華陽',21);
完成插入之後,zhiguoxin 的表格内容如下:
idnameage1001劉堯181002聶衍文191003楊佳晨201004馮華陽21
3.6 查詢表中的資料
//查詢表中的所有資料
select * from 表名;
//select * from zhiguoxin;
3.7 檢視資料庫
可以把first.db資料庫檔案拷貝至windows下,使用SQLite Developer打開即可看到。SQLite Developer下載下傳位址
https://mydown.yesky.com/pcsoft/443425.html
3.8 按條件查找
1.使用where指定查詢條件
select * from zhiguoxin where id=1003;//查詢id值為1003的條目
select * from zhiguoxin where age>=19 and age<21;
select * from zhiguoxin where age>=19 or age<21;
2.指定查詢的字段
select id,name,age from zhiguoxin;//隻查詢id,name,age的字段
3.使用where+like實作模糊查詢
select * from zhiguoxin where name like '劉%';//查找名字以劉開頭的條目
4.使用order by實作查詢結果按某個字段的值升序/降序輸出
select * from zhiguoxin order by age desc;//按年齡降序排序
select * from zhiguoxin order by id asc; //按id升序排序
3.9 删除表中的條目delete from 表名 where 條件;//删除所有符合條件的條目
比如:
delete from zhiguoxin where id=1001;
3.10 更新(修改)表中的條目update 表名 set 字段名1=字段值1,字段名2=字段值2... where 條件;//修改符合條件的條目
比如:
update zhiguoxin set age=100 where id=1002;
嵌入式物聯網需要學的東西真的非常多,千萬不要學錯了路線和内容,導緻工資要不上去!
無償分享大家一個資料包,差不多150多G。裡面學習内容、面經、項目都比較新也比較全!某魚上買估計至少要好幾十。
點選這裡找小助理0元領取:加微信領取資料
3.11 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的庫。
4.1 打開 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
4.2 關閉 sqlite3_close
int sqlite3_close(sqlite3 *pDb);
//傳入要關閉的資料庫的句柄
4.3 編譯方法
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)
4.4 執行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,否則執行失敗
幾個例子
//連接配接資料庫
int Connection_Sqlite3DataBase()
{
rc = sqlite3_open("./face_database/face.db", &db);
if (rc != SQLITE_OK)
{
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else
printf("You have opened a sqlite3 database named bind.db successfully!\nCongratulation! Have fun!\n");
return 0;
}
//将圖檔插入到資料庫
void insert_face_data_toDataBase(const char *name, MByte *face_feature, MInt32 featureSize)
{
sqlite3_prepare(db, "insert into face_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);
}
文章連結:
https://mp.weixin.qq.com/s/xPICKBDMWN-9dOimDCwzIw
轉載自:果果小師弟,作者智果芯
文章連結:手把手教你在嵌入式裝置中使用SQLite3