嵌入式數(shù)據(jù)庫是什么
時間:2018-03-08作者:華清遠(yuǎn)見
SQLite,是一款輕型的數(shù)據(jù)庫,其設(shè)計目標(biāo)是嵌入式的數(shù)據(jù)庫,而且當(dāng)前在很多嵌入式產(chǎn)品中使用到了sqlite數(shù)據(jù)庫,sqlite數(shù)據(jù)庫占用資源非常的低,對嵌入式設(shè)備而言,內(nèi)存是非常寶貴的,而sqlite數(shù)據(jù)庫可能只需要幾百K的內(nèi)存就夠了。 Sqlite數(shù)據(jù)庫還能夠支持Windows/Linux/Unix等等主流的操作系統(tǒng),其跨平臺的可移植性特別強(qiáng),這極大的拓展了其生存的空間。 同時能夠跟很多程序語言相結(jié)合,比如C#、PHP、Java,C等,還有ODBC接口,同樣比起Mysql、PostgreSQL這兩款開源的世界著名數(shù)據(jù)庫管理系統(tǒng)來講,它的處理速度比他們都快,比起Oracle數(shù)據(jù)庫來說,免費也是極大的優(yōu)勢。 SQLite第一個Alpha版本誕生于2000年5月,至2018年已經(jīng)有18個年頭, SQLite 3也已經(jīng)發(fā)布,并成功的進(jìn)入了我們的事業(yè)。 除了上面提到的低內(nèi)存占用率、跨平臺可移植性、多語言支持、免費開源,sqlite3數(shù)據(jù)庫還有強(qiáng)大的功能,比如支持?jǐn)?shù)據(jù)庫大小可達(dá)2TB,十多萬行的代碼非常適合學(xué)習(xí),良好的代碼注釋等等。 那么我們學(xué)習(xí)數(shù)據(jù)庫最好可以快速的入門,在門外徘徊太久的話會挫傷我們學(xué)習(xí)的積極性。如果你是一個嵌入式的學(xué)習(xí)和開發(fā)者,那么從以下兩個方面出發(fā)可以幫你快速的上手sqlite3數(shù)據(jù)庫: 1、Sqlite3的命令 :數(shù)據(jù)庫的創(chuàng)建及增刪改查 2、2、sqlite3 API函數(shù)接口:用API接口實現(xiàn)數(shù)據(jù)庫的創(chuàng)建及增刪改查 當(dāng)然了,以下是基于Ubuntu平臺做的測試: 1-- 安裝數(shù)據(jù)庫: 在線安裝: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 (數(shù)據(jù)庫安裝文件) 2、Sqlite3的命令 2.1、創(chuàng)建數(shù)據(jù)庫 sqlite3 stu.db 必須指定數(shù)據(jù)庫名字 2.2、sqlite命令 系統(tǒng)命令 以 "."開頭 普通命令 ,以";"結(jié)束 .schema 查看表的結(jié)構(gòu) .quit 退出數(shù)據(jù)庫 .exit 退出數(shù)據(jù)庫 .help 查看幫助信息 .databases 查看數(shù)據(jù)庫 .tables 顯示數(shù)據(jù)庫中所有的表的表名 2.3、sqlite3 的使用 : 增刪改查 1-- 創(chuàng)建一張表 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)先創(chuàng)建一張新表 create table stu1 as select id , name from stu; 2)刪除原來的舊表 drop table stu; 3)對新表重命名 alter table stu1 rename to stu; 9-- 數(shù)據(jù)庫主鍵(既設(shè)置的數(shù)據(jù)將會是唯一存在的) create table usr(name text primary key , passwd text); 3、sqlite3 API 函數(shù)接口: 學(xué)習(xí)API函數(shù)接口,那么首先要掌握函數(shù)接口的三要素: 功能--參數(shù)--返回值, 然后就是寫代碼去驗證我們隊函數(shù)的理解,然后將函數(shù)用到實際的項目開發(fā)當(dāng)中 (1)int sqlite3_open( const char *filename, /* Database filename (UTF-8) */ sqlite3 **ppDb /* OUT: SQLite db handle */ ); 功能:打開一個數(shù)據(jù)庫 參數(shù):filename 數(shù)據(jù)庫名字 ppdb 操作數(shù)據(jù)庫的指針,句柄。 返回值:成功 SQLITE_OK , 失敗 error_code (2)const char *sqlite3_errmsg(sqlite3* db); 功能:獲取錯誤信息描述 (3)int sqlite3_close(sqlite3* db); 功能:關(guān)閉一個數(shù)據(jù)庫 (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 */ ); 功能:執(zhí)行一條sql語句 參數(shù):db 數(shù)據(jù)庫的句柄指針 sql 將要被執(zhí)行sql語句 callback 回調(diào)函數(shù), 只有在查詢語句時,才給回調(diào)函數(shù)傳參 arg 為callback 傳參的 errmsg 錯誤信息的地址 返回值:成功 SQLITE_OK 出錯 errcode 錯誤碼 int (*callback)(void* arg ,int ncolumn ,char** f_value,char** f_name) 功能:得到查詢結(jié)果 參數(shù):arg 為回調(diào)函數(shù)傳遞參數(shù)使用的 ncolumn 記錄中包含的字段的數(shù)目 f_value 包含每個字段值的指針數(shù)組 f_name 包含每個字段名稱的指針數(shù)組 返回值:成功 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 */ ); 功能:查詢數(shù)據(jù)庫,它會創(chuàng)建一個新的內(nèi)存區(qū)域來存放查詢的結(jié)果信息 參數(shù):db 數(shù)據(jù)庫操作句柄 sql 數(shù)據(jù)庫的sql語句 azResult 查詢的結(jié)果 nRow 行數(shù) nColumn 列數(shù) errmsg 錯誤消息 返回值: 成功 0 出錯 errcode nrow的值為查詢到的符合條件的記錄數(shù)(不包括字段名)。 ncolumn的值為查詢到的符合條件的字段數(shù)。 注意:nrow的值不包括字段名,如果打印時用for (i = 0; i < nrow; i++)會打印出字段名,但是會少打印出一條符合條件的記錄。 因此打印時要用 for (i = 0; i (6)void sqlite3_free_table(char **result); 功能:釋放內(nèi)存 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函數(shù)接口的使用 測試源碼:
#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; } //回調(diào)函數(shù)負(fù)責(zé)得到查詢的結(jié)果 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; } //查詢數(shù)據(jù) 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; //創(chuàng)建(打開)數(shù)據(jù)庫 if(sqlite3_open(DATABASE, &db) != SQLITE_OK) { printf("%s\n", sqlite3_errmsg(db)); }else{ printf("the database is opened successfully\n"); } //創(chuàng)建一張表 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"); } //對當(dāng)前表進(jìn)行增刪改查 while(1) { printf("\n"); printf("***1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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; } 測試結(jié)果: fengjunhui@ubuntu:~/Sqlite$ ./a.out the database is opened successfully table stu already exists 1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 5:退出 >>> 2 id name score 1001 liuxiaofan 92 1004 fengjunhui 98 1003 luzhengyu 96 the datas is selected successfully 1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 5:退出 >>> 1 please input id >>> 1005 please input name >>> xiaohuihui please input score >>> 76 the datas is inserted successfully 1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 5:退出 >>> 2 id name score 1001 liuxiaofan 92 1004 fengjunhui 88 1003 luzhengyu 96 1005 xiaohuihui 76 the datas is selected successfully 1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 5:退出 >>> 2 id name score 1001 liuxiaofan 92 1004 fengjunhui 88 1003 luzhengyu 96 1005 xiaohuihui 76 the datas is selected successfully 1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 5:退出 >>> 2 id name score 1001 liuxiaofan 92 1003 luzhengyu 96 1005 xiaohuihui 76 the datas is selected successfully 1:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 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:插入數(shù)據(jù) 2:查詢數(shù)據(jù) 3:修改數(shù)據(jù) 4:刪除數(shù)據(jù) 5:退出 >>> 5 擴(kuò)展內(nèi)容: sqlite3支持的數(shù)據(jù)類型: NULL、INTEGER、REAL、TEXT、BLOB 但是,sqlite3也支持如下的數(shù)據(jù)類型 smallint 16位整數(shù) integer 32位整數(shù) decimal(p,s) p是精確值,s是小數(shù)位數(shù) float 32位實數(shù) double 64位實數(shù) char(n) n長度字符串,不能超過254 varchar(n) 長度不固定最大字符串長度為n,n不超過4000 graphic(n) 和 char(n) 一樣,但是單位是兩個字符double-bytes,n不超過127(中文字) vargraphic(n) 可變長度且最大長度為n date 包含了年份、月份、日期 time 包含了小時、分鐘、秒 timestamp 包含了年、月、日、時、分、秒、千分之一秒 sqlite3支持的函數(shù) 【1】日期函數(shù) datetime() : 產(chǎn)生日期和時間 date(): 產(chǎn)生日期 time():產(chǎn)生時間 strftime():對以上3個函數(shù)產(chǎn)生的日期和時間進(jìn)行格式化 用法實例: 1、select date('2011-9-9','+1 day','+1 year'); 結(jié)果是 2010-09-10 2、select datetime('now'); 當(dāng)前日期和時間 3、select datetime('now', 'start of month'); 本月的第一天零點,也可以設(shè)置年和日的第一天 4、select datetime('now','+1 hour','-12 minute'); 當(dāng)前時間加48分鐘 strftime()函數(shù)可以將YYYY-MM-DD HH:MM:SS格式的日期字符串轉(zhuǎn)換為其它形式的字符串 %d:天數(shù),01-31 %f :小數(shù)形式的秒,SS.SSS %H:小時 %j :某一天是該年的第幾天,001-366 %m:月份,00-12 %M:分鐘,00-59 %s:從1970到現(xiàn)在的秒數(shù) %S:秒,00-59 %w:星期,0-6,0是星期天 %W:某天是該年的第幾周,01-53 %Y:年,YYYY %% 百分號 應(yīng)用舉例: select strftime('%Y.%m.%d %H:%M:%S','now','localtime'); 二、【算術(shù)函數(shù)】 abs(X):返回絕對值 max(X,Y[,...]):返回最大值 min(X,Y,[,...]):返回最小值 random(*):返回隨機(jī)數(shù) round(X[,Y]): 四舍五入 三、【字符串處理函數(shù)】 length(x) :返回字符串字符個數(shù) lower(x) :大寫轉(zhuǎn)小寫 upper(x):小寫轉(zhuǎn)大寫 substr(x,y,Z):截取子串 like(A,B):確定給定的字符串與指定的模式是否匹配 相關(guān)資訊
發(fā)表評論
|
全國咨詢電話:400-611-6270,雙休日及節(jié)假日請致電值班手機(jī):15010390966
在線咨詢: 曹老師QQ(3337544669), 徐老師QQ(1462495461), 劉老師 QQ(3108687497)
企業(yè)培訓(xùn)洽談專線:010-82600901,院校合作洽談專線:010-82600350,在線咨詢:QQ(248856300)
Copyright 2004-2018 華清遠(yuǎn)見教育科技集團(tuán) 版權(quán)所有 ,京ICP備16055225號,京公海網(wǎng)安備11010802025203號