色yeye在线视频观看_亚洲人亚洲精品成人网站_一级毛片免费播放_91精品一区二区中文字幕_一区二区三区日本视频_成人性生交大免费看

當前位置:首頁 > 嵌入式培訓 > 嵌入式學習 > 講師博文 > 嵌入式數據庫是什么

嵌入式數據庫是什么 時間:2018-02-23      來源:未知

SQLite,是一款輕型的數據庫,其設計目標是嵌入式的數據庫,而且當前在很多嵌入式產品中使用到了sqlite數據庫,sqlite數據庫占用資源非常的低,對嵌入式設備而言,內存是非常寶貴的,而sqlite數據庫可能只需要幾百K的內存就夠了。

Sqlite數據庫還能夠支持Windows/Linux/Unix等等主流的操作系統,其跨平臺的可移植性特別強,這極大的拓展了其生存的空間。

同時能夠跟很多程序語言相結合,比如C#、PHP、Java,C等,還有ODBC接口,同樣比起Mysql、PostgreSQL這兩款開源的世界著名數據庫管理系統來講,它的處理速度比他們都快,比起Oracle數據庫來說,免費也是極大的優勢。

SQLite第一個Alpha版本誕生于2000年5月,至2018年已經有18個年頭, SQLite 3也已經發布,并成功的進入了我們的事業。

除了上面提到的低內存占用率、跨平臺可移植性、多語言支持、免費開源,sqlite3數據庫還有強大的功能,比如支持數據庫大小可達2TB,十多萬行的代碼非常適合學習,良好的代碼注釋等等。

那么我們學習數據庫好可以快速的入門,在門外徘徊太久的話會挫傷我們學習的積極性。如果你是一個嵌入式的學習和開發者,那么從以下兩個方面出發可以幫你快速的上手sqlite3數據庫:

1、Sqlite3的命令 :數據庫的創建及增刪改查

2、2、sqlite3 API函數接口:用API接口實現數據庫的創建及增刪改查

當然了,以下是基于Ubuntu平臺做的測試:

1-- 安裝數據庫:

在線安裝:sudo apt-get install sqlite sqlite3

sudo apt-get install libsqlite3-dev

sudo apt-get install sqlitebrowser

離線安裝:

下載安裝下面三個包:

libsqlite3-0_3.7.2-1ubuntu0.1_i386.deb

libsqlite3-dev_3.7.2-1ubuntu0.1_i386.deb

sqlite3_3.7.2-1ubuntu0.1_i386.deb

sudo dpkg -i *.deb  (數據庫安裝文件)

2、Sqlite3的命令

2.1、創建數據庫

sqlite3 stu.db

必須指定數據庫名字

2.2、sqlite命令

系統命令 以 "."開頭

普通命令 ,以";"結束

.schema 查看表的結構

.quit 退出數據庫

.exit 退出數據庫

.help 查看幫助信息

.databases 查看數據庫

.tables 顯示數據庫中所有的表的表名

2.3、sqlite3 的使用 : 增刪改查

1-- 創建一張表

create table 表名(字段名稱1 字段類型,字段名稱2 字段類型, ....);

create table stu(id int, name char, sex char , score int);

2-- 向表中插入一條記錄

insert into 表名 values (字段值1,字段值2,...);

insert into stu values(1001, 'zhangsan', 'm', 89);

insert into stu (id, name, sex,score) values(1002, 'lisi', 'm', 99);

3-- 查詢記錄

select * from stu; // 查找所有的記錄

select * from stu where id=1001; // 查找符號條件的記錄

select * from stu where id=1001 and name='zhangsan'; // 字符串需要加引號

select * from stu where name = 'zhangsan' or score=92;

4-- 刪除記錄

delete from stu where id=1004;

5-- 更新記錄

update stu set score=98 where id=1003;

6-- 刪除一張表

drop table stu;

7-- 添加一列

alter table stu add column score int;

8-- 刪除一列

sqlite3 不允許直接刪除一列

1)先創建一張新表

create table stu1 as select id , name from stu;

2)刪除原來的舊表

drop table stu;

3)對新表重命名

alter table stu1 rename to stu;

9-- 數據庫主鍵(既設置的數據將會是唯一存在的)

create table usr(name text primary key , passwd text);

3、sqlite3 API 函數接口:

學習API函數接口,那么首先要掌握函數接口的三要素: 功能--參數--返回值,

然后就是寫代碼去驗證我們隊函數的理解,然后將函數用到實際的項目開發當中

(1)int sqlite3_open(

const char *filename, /* Database filename (UTF-8) */

sqlite3 **ppDb /* OUT: SQLite db handle */

);

功能:打開一個數據庫

參數:filename 數據庫名字

ppdb 操作數據庫的指針,句柄。

返回值:成功 SQLITE_OK , 失敗 error_code

(2)const char *sqlite3_errmsg(sqlite3* db);

功能:獲取錯誤信息描述

(3)int sqlite3_close(sqlite3* db);

功能:關閉一個數據庫

(4)int sqlite3_exec(

sqlite3* db, /* An open database */

const char *sql, /* SQL to be evaluated */

int (*callback)(void*,int,char**,char**), /* Callback function */

void * arg, /* 1st argument to callback */

char **errmsg /* Error msg written here */

);

功能:執行一條sql語句

參數:db 數據庫的句柄指針

sql 將要被執行sql語句

callback 回調函數, 只有在查詢語句時,才給回調函數傳參

arg 為callback 傳參的

errmsg 錯誤信息的地址

返回值:成功 SQLITE_OK

出錯 errcode 錯誤碼

int (*callback)(void* arg ,int ncolumn ,char** f_value,char** f_name)

功能:得到查詢結果

參數:arg 為回調函數傳遞參數使用的

ncolumn 記錄中包含的字段的數目

f_value 包含每個字段值的指針數組

f_name 包含每個字段名稱的指針數組

返回值:成功 0,出錯 非0

(5)int sqlite3_get_table(

sqlite3 *db, /* An open database */

const char *zSql, /* SQL to be evaluated */

char ***pazResult, /* Results of the query */

int *pnRow, /* Number of result rows written here */

int *pnColumn, /* Number of result columns written here */

char **pzErrmsg /* Error msg written here */

);

功能:查詢數據庫,它會創建一個新的內存區域來存放查詢的結果信息

參數:db 數據庫操作句柄

sql 數據庫的sql語句

azResult 查詢的結果

nRow 行數

nColumn 列數

errmsg 錯誤消息

返回值:

成功 0

出錯 errcode

nrow的值為查詢到的符合條件的記錄數(不包括字段名)。

ncolumn的值為查詢到的符合條件的字段數。

注意:nrow的值不包括字段名,如果打印時用for (i = 0; i < nrow; i++)會打印出字段名,但是會少打印出一條符合條件的記錄。

因此打印時要用 for (i = 0; i

(6)void sqlite3_free_table(char **result);

功能:釋放內存

4、下面我們先來測一下sqlite命令的使用:

fengjunhui@ubuntu:~/Sqlite$ sqlite3 fengjunhui.db

SQLite version 3.7.2

Enter ".help" for instructions

Enter SQL statements terminated with a ";"

sqlite> create table stu(id int,name char,sex char,score int);

sqlite> insert into stu values(1001,'fengjunhui','m',88);

1001|fengjunhui|m|88

1002|liuxiaofan|m|92

1003|luzhengyu|m|96

1004|xiaohui|m|86

sqlite> select * from stu where name=fengjunhui ;

Error: no such column: fengjunhui         ----注意類型匹配

sqlite> select * from stu where name = 'fengjunhui';

1001|fengjunhui|m|88

sqlite> select * from stu where id=1003;

1003|luzhengyu|m|96

sqlite> update stu set sex = f where id=1002;

Error: no such column: f

sqlite> update stu set sex ='f' where id=1002;

sqlite> select * from stu

...> ;

1001|fengjunhui|m|88

1002|liuxiaofan|f|92

1003|luzhengyu|m|96

1004|xiaohui|m|86

sqlite> delete from stu where name='xiaohui';

sqlite> select * from stu;

1001|fengjunhui|m|88

1002|liuxiaofan|f|92

1003|luzhengyu|m|96

sqlite> alter table stu add column age int;

sqlite> select * from stu ;

1001|fengjunhui|m|88|

1002|liuxiaofan|f|92|

1003|luzhengyu|m|96|

sqlite> update stu set age = 25 where id=1001;

sqlite> select * from stu;

1001|fengjunhui|m|88|25

1002|liuxiaofan|f|92|

1003|luzhengyu|m|96|

sqlite> create table stu_bak as select id,name,sex,score from stu;

sqlite> .table

stu stu_bak

sqlite> .schema

CREATE TABLE stu(id int,name char,sex char,score int, age int);

CREATE TABLE stu_bak(

id INT,

name TEXT,

sex TEXT,

score INT

);

sqlite> drop table stu;

sqlite> alter table stu_bak rename to stu;

sqlite> .table

stu

sqlite> create table usr(name text primary key,passwd text);

sqlite> .talbe

Error: unknown command or invalid arguments: "talbe". Enter ".help" for help

sqlite> .table

stu usr

5、sqlite3 API函數接口的使用

測試源碼:

#include

#include

//注意:編譯時需要手動鏈接庫文件 -lsqlite3

#define DATABASE "student.db"

#define N 128

#define IS_NUM(index) (0 <= index && index <= 9) ? 1 : 0

int flags = 0;

int do_insert(sqlite3 *db)

{

int id;

char name[N];

int score;

char sql[N];

char *errmsg;

printf("please input id >>> ");

scanf("%d", &id);

getchar();

printf("please input name >>> ");

scanf("%s", name);

getchar();

printf("please input score >>> ");

scanf("%d", &score);

getchar();

sprintf(sql, "insert into stu values(%d, '%s', %d)", id, name, score);

if(sqlite3_exec(db, sql, NULL, NULL, &errmsg) != SQLITE_OK)

{

printf("%s\n", errmsg);

}

else

{

printf("the datas is inserted successfully\n");

}

return 0;

}

//回調函數負責得到查詢的結果

int callback(void *arg, int ncolumn, char **f_value, char **f_name)

{

int i = 0;

if(flags == 0)

{

for(i = 0; i < ncolumn; i++)

{

printf("%-11s", f_name[i]);

}

putchar(10);

flags = 1;

}

for(i = 0; i < ncolumn; i++)

{

printf("%-11s", f_value[i]);

}

putchar(10);

return 0;

}

//查詢數據

int do_select(sqlite3 *db)

{

char *errmsg;

if(sqlite3_exec(db, "select * from stu", callback, NULL, &errmsg) != SQLITE_OK){

printf("%s\n", errmsg);

}else{

printf("the datas is selected successfully\n");

}

return 0;

}

int do_update(sqlite3* db)

{

int id,score;

char name[N]={0},columnname[N]={0},columntype[N]={0};

int num,index;

char* errmsg;

char sql[N]={0};

printf("input your index for search: 1 id 2 name 3 score: \n");

scanf("%d",&index);

getchar();

if(index == 1){

printf("input id : \n");

scanf("%d",&id);

}else if(index == 2){

printf("input name : \n");

scanf("%s",name);

}else {

printf("input score: \n");

scanf("%d",&score);

}

printf("your choice whose info to update: 1 id 2 name 3 score 4 colum: \n");

scanf("%d",&num);

getchar();

switch(num)

{

case 1:

printf("please input id: ");

scanf("%d",&id);

getchar();

if(index == 1){

printf("input sorry,the same info,no need change.\n ");

}else if(index == 2){

sprintf(sql,"update stu set id=%d where name='%s'",id,name);

}else if(index == 3){

sprintf(sql,"update stu set id=%d where score=%d",id,score);

}if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){

printf("%s\n",errmsg);

}

break;

case 2:

printf("please input name: ");

scanf("%s",name);

getchar();

if(index == 1){

sprintf(sql,"update stu set name=%s where id='%d'",name,id);

}if(index == 2){

printf("input sorry,the same info,no need change.\n ");

}else if(index == 3){

sprintf(sql,"update stu set name=%s where score=%d",name,score);

}if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){

printf("%s\n",errmsg);

}

break;

case 3:

printf("please input socre: ");

scanf("%d",&score);

getchar();

if(index == 1) {

sprintf(sql,"update stu set score=%d where id='%d'",score,id);

}else if(index == 2){

sprintf(sql,"update stu set score=%d where name=%s",score,name);

} else if(index == 3){

printf("input sorry,the same info,no need change.\n ");

}

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){

printf("%s\n",errmsg);

}

break;

case 4:

printf("please input column name: ");

scanf("%s",columnname);

getchar();

printf("please input column type: INT or CHAR");

scanf("%s",columntype);

getchar();

sprintf(sql,"alter table stu add column '%s' '%s' '",columnname,columntype);

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){

printf("%s\n",errmsg);

}

break;

default:

printf("input illegal.\n");

}

printf("the datas is update successfully\n");

return 0;

}

int do_delete(sqlite3* db)

{

int i=0;

char table[N]={0};

int id,score,index;

char name[N]={0};

char* errmsg;

char sql[N]={0};

char *delsql[5]={0};

printf("\n");

do_select(db); //printf database info list

printf("\n");

printf("input index for delete: 1 id 2 name 3 score 4 table 5 colum: \n");

scanf("%d",&index);

getchar();

if(index == 1){

printf("input id : \n");

scanf("%d",&id);

sprintf(sql,"delete from stu where id=%d",id);

}else if(index == 2){

printf("input name : \n");

scanf("%s",name);

sprintf(sql,"delete from stu where name=%s",name);

}else if(index == 3){

printf("input score: \n");

scanf("%d",&score);

sprintf(sql,"delete from stu where score=%d",score);

}else if(index == 4){

printf("input which table: \n");

scanf("%s",table);

sprintf(sql,"drop table %s",table);

}else if(index == 5){

#if 0

sprintf(delsql[0],"create table stu1 as select id,name from stu\n");

sprintf(delsql[1],"drop table stu\n");

sprintf(delsql[2],"alter table stu1 rename to stu\n");

#endif

delsql[0] = "create table stu1 as select id,name from stu";

delsql[1] = "drop table stu";

delsql[2] = "alter table stu1 rename to stu";

for(i = 0;i < 3;i ++){

printf("delsql[%d]: %s\n",i,delsql[i]);

}

return 0;

}

if(sqlite3_exec(db,sql,NULL,NULL,&errmsg) != SQLITE_OK){

printf("%s\n",errmsg);

}

return 0;

}

int main(int argc, const char *argv[])

{

sqlite3 *db;

char *errmsg;

int num;

//創建(打開)數據庫

if(sqlite3_open(DATABASE, &db) != SQLITE_OK) {

printf("%s\n", sqlite3_errmsg(db));

}else{

printf("the database is opened successfully\n");

}

//創建一張表

if(sqlite3_exec(db, "create table stu(id int, name char, score int)", NULL, NULL, &errmsg) != SQLITE_OK){

printf("%s\n", errmsg);

}else {

printf("the table is created successfully\n");

}

//對當前表進行增刪改查

while(1)

{

printf("\n");

printf("***1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出***\n");

printf("\n");

printf(">>> ");

scanf("%d", &num);

getchar();

switch(num)

{

case 1:

do_insert(db);

break;

case 2:

flags = 0;

do_select(db);

//do_select_get_table(db);

break;

case 3:

do_update(db);

break;

case 4:

do_delete(db);

break;

case 5:

sqlite3_close(db);

return -1;

default:

printf("please input correct option\n");

}

}

return 0;

}

測試結果:

fengjunhui@ubuntu:~/Sqlite$ ./a.out

the database is opened successfully

table stu already exists

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 2

id name score

1001 liuxiaofan 92

1004 fengjunhui 98

1003 luzhengyu 96

the datas is selected successfully

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 1

please input id >>> 1005

please input name >>> xiaohuihui

please input score >>> 76

the datas is inserted successfully

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 3

input your index for search: 1 id 2 name 3 score:

1

input id :

1004

your choice whose info to update: 1 id 2 name 3 score 4 colum:

3

please input socre: 88

the datas is update successfully

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 2

id name score

1001 liuxiaofan 92

1004 fengjunhui 88

1003 luzhengyu 96

1005 xiaohuihui 76

the datas is selected successfully

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 4

1001 liuxiaofan 92

1004 fengjunhui 88

1003 luzhengyu 96

1005 xiaohuihui 76

the datas is selected successfully

input index for delete: 1 id 2 name 3 score 4 table 5 colum:

2

input name :

fengjunhui

no such column: fengjunhui

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 2

id name score

1001 liuxiaofan 92

1004 fengjunhui 88

1003 luzhengyu 96

1005 xiaohuihui 76

the datas is selected successfully

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 5

fengjunhui@ubuntu:~/Sqlite$ gcc 5_student.c -lsqlite3

fengjunhui@ubuntu:~/Sqlite$ ./a.out

the database is opened successfully

table stu already exists

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 4

id name score

1001 liuxiaofan 92

1004 fengjunhui 88

1003 luzhengyu 96

1005 xiaohuihui 76

the datas is selected successfully

input index for delete: 1 id 2 name 3 score 4 table 5 colum:

2

input name :

'fengjunhui'

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 2

id name score

1001 liuxiaofan 92

1003 luzhengyu 96

1005 xiaohuihui 76

the datas is selected successfully

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 5

fengjunhui@ubuntu:~/Sqlite$ gcc 5_student.c -lsqlite3

fengjunhui@ubuntu:~/Sqlite$ ./a.out

the database is opened successfully

table stu already exists

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 4

id name score

1001 liuxiaofan 92

1003 luzhengyu 96

1005 xiaohuihui 76

the datas is selected successfully

input index for delete: 1 id 2 name 3 score 4 table 5 colum:

5

delsql[0]: create table stu1 as select id,name from stu

delsql[1]: drop table stu

delsql[2]: alter table stu1 rename to stu

1:插入數據 2:查詢數據 3:修改數據 4:刪除數據 5:退出

>>> 5

擴展內容:

sqlite3支持的數據類型:

NULL、INTEGER、REAL、TEXT、BLOB

但是,sqlite3也支持如下的數據類型

smallint 16位整數

integer 32位整數

decimal(p,s) p是精確值,s是小數位數

float 32位實數

double 64位實數

char(n) n長度字符串,不能超過254

varchar(n) 長度不固定大字符串長度為n,n不超過4000

graphic(n) 和 char(n) 一樣,但是單位是兩個字符double-bytes,n不超過127(中文字)

vargraphic(n) 可變長度且大長度為n

date 包含了年份、月份、日期

time 包含了小時、分鐘、秒

timestamp 包含了年、月、日、時、分、秒、千分之一秒

sqlite3支持的函數

【1】日期函數

datetime() : 產生日期和時間

date(): 產生日期

time():產生時間

strftime():對以上3個函數產生的日期和時間進行格式化

用法實例:

1、select date('2011-9-9','+1 day','+1 year'); 結果是 2010-09-10

2、select datetime('now'); 當前日期和時間

3、select datetime('now', 'start of month'); 本月的第一天零點,也可以設置年和日的第一天

4、select datetime('now','+1 hour','-12 minute'); 當前時間加48分鐘

strftime()函數可以將YYYY-MM-DD HH:MM:SS格式的日期字符串轉換為其它形式的字符串

%d:天數,01-31

%f :小數形式的秒,SS.SSS

%H:小時

%j :某一天是該年的第幾天,001-366

%m:月份,00-12

%M:分鐘,00-59

%s:從1970到現在的秒數

%S:秒,00-59

%w:星期,0-6,0是星期天

%W:某天是該年的第幾周,01-53

%Y:年,YYYY

%% 百分號

應用舉例:

select strftime('%Y.%m.%d %H:%M:%S','now','localtime');

二、【算術函數】

abs(X):返回絕對值

max(X,Y[,...]):返回大值

min(X,Y,[,...]):返回小值

random(*):返回隨機數

round(X[,Y]): 四舍五入

三、【字符串處理函數】

length(x) :返回字符串字符個數

lower(x) :大寫轉小寫

upper(x):小寫轉大寫

substr(x,y,Z):截取子串

like(A,B):確定給定的字符串與指定的模式是否匹配

上一篇:linux和ubuntu的區別在哪

下一篇:JTAG接口

熱點文章推薦
華清學員就業榜單
高薪學員經驗分享
熱點新聞推薦
前臺專線:010-82525158 企業培訓洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠見科技集團有限公司 版權所有 ,京ICP備16055225號-5京公海網安備11010802025203號

回到頂部

主站蜘蛛池模板: 欧美videosdesexo吹潮 | 人妻少妇被猛烈进入中文字幕 | 欧美毛片无码又大又粗黑寡妇 | 永久免费av无码不卡在线观看 | 久久国产成人精品av | 亚洲精品一区人人爽 | 日韩 欧美 精品 | 日本少妇高潮正在线播放 | 国产亚洲日韩在线一区二区三区 | 成人无码h肉动漫在线观看站 | 日韩乱码人妻无码系列中文字幕 | 亚洲欧洲日产国码中文字幕 | 国产精品天干天干综合网 | 亚洲人成影院在线无码按摩店 | 日日AV色欲香天天综合网 | 中文字幕AV一区乱码 | 国产又粗又猛又爽又黄的视频在线观看动漫 | 又粗又大又硬毛片免费看 | 亚洲精品456在线观看 | 老司机精品视频一区二区 | 性色AV网站| av无码天堂一区二区三区, | 丁香五月激情综合激情综合激情 | 亚洲国产一区二区a毛片日本 | 国产乱妇无码大片在线观看 | 亚洲精品无码久久久影院相关影片 | 射婷婷| 国精品人妻无码一区二区三区性色 | 国产十八禁真成了 | 国产精品V无码A片在线看 | 国产人妻精品午夜福利免费 | 国产传媒在线免费观看 | 久久久久无码国产精品不卡 | 少妇宾馆把腿扒开让我添 | 亚洲精品无码一区二区三区污 | 极品人妻老师的娇喘呻吟 | 艳妇乳肉豪妇荡乳AV无码福利 | 不卡高清AV手机在线观看 | 欧美岛国国产 | 中文一国产一无码一日韩 | 在线免费观看日本欧美 |