什麼叫嵌入式資料庫?

germany006發表於2014-04-13
轉載地址:http://baike.baidu.com/link?url=G0DG75wi7jjQxa5HOvRGEUClzqxzN-EhWU_-SgDnOu2H9cAx9QJejcOwLuxbqQY9kjUj972sgPuV_xCzRV2q_a

嵌入式資料庫

嵌入式資料庫的名稱來自其獨特的執行模式。這種資料庫嵌入到了應用程式程式中,消除了與客戶機伺服器配置相關的開銷。嵌入式資料庫實際上是輕量級的,在執行時,它們需要較少的記憶體。它們是使用精簡程式碼編寫的,對於嵌入式裝置,其速度更快,效果更理想。嵌入式執行模式允許嵌入式資料庫通過 SQL 來輕鬆管理應用程式資料,而不依靠原始的文字檔案。嵌入式資料庫還提供零配置執行模式,這樣可以啟用其中一個並執行一個快照。
1. 嵌入式資料庫介紹
嵌入式系統中,對資料庫的操作具有定時限制的特性,這裡把應用於嵌入式系統資料庫系統稱為嵌入式資料庫系統或嵌入式實時資料庫系統(ERTDBS)。
可靠性要求是毋庸置疑的,嵌入式系統必須能夠在沒有人工干預的情況下,長時間不間斷地執行。同時要求資料庫操作具備可預知性,而且系統的大小和效能也都必須是可預知的,這樣才能保證系統的效能。嵌入式系統中會不可避免地與底層硬體打交道,因此在資料管理時,也要有底層控制的能力,如什麼時候會發生磁碟操作,磁碟操作的次數,如何控制等。底層控制的能力是決定資料庫管理操作的關鍵。
目前嵌入式軟體系統開發的挑戰之一,體現在對各種資料的管理能否建立一套可靠、高效、穩定的管理模式,嵌入式資料庫可謂應運而生。
嵌入式資料庫是嵌入式系統的重要組成部分,也成為對越來越多的個性化應用開發和管理而採用的一種必不可少的有效手段。
嵌入式資料庫用途廣泛,如用於消費電子產品、移動計算裝置、企業實時管理應用、網路儲存與管理以及各種專用裝置,這一市場目前正處於高速增長之中。 舉簡單例子,手機原來只用來打電話、發簡訊,現在手機增加了很多新的功能,比如彩信、音樂、攝影、視訊等等,應用的功能多了,系統就變得複雜。

1常用的嵌入式資料庫的比較編輯

2.1 Progress

Progress軟體公司2000年4月18號18時在京宣佈,全面發售在Linux作業系統上執行的資料庫及其部署產品。Progress在嵌入式資料庫市場中擁有全球第一的佔有率,世界上有超過200萬人正在使用Progress軟體公司的應用軟體,目前部署Progress產品的站點數量已經超過100,000個。通過Progress軟體公司第一個Linux版嵌入式資料庫,獨立軟體開發商和終端使用者可以在這一流行的作業系統上移植5,000多種商業應用。
----Progress軟體公司當時推出的產品為ProgressVersion8.3,現在已經到了10.2c版本。這是一套完善的整合開發工具、應用伺服器關係型資料庫產品,提供了可擴充的多層Linux支援。Progress軟體公司的Linux專用產品包括:
Progress(r)AppServer(tm):這是一種可以在異構環境中部署共享應用元件的應用伺服器Progress(r)EnterpriseRDBMS(tm):為需要支援大型資料庫、多處理器硬體和數千個併發使用者的最苛刻的應用提供了一種可擴充的儲存解決方案。
----用於RedHat6.0Linux的ProgressVersion8.3部署產品現已全面上市。Progress已推出用於Linux的ProgressVersion9、Progress(r)WebSpeed(r)Version3、Progress(r)Apptivity(tm)和Progress(r)SonicMQ(tm)部署產品。
----目前全球頂尖的汽車行業ERP供應商QAD支援最新的PROGRESS版本。

2.2 SQLite

輕量級別資料庫SQLite的主要特點:
1. 支援事件,不需要配置,不需要安裝,也不需要管理員;
2. 支援大部分SQL92;
3. 一個完整的資料庫儲存在磁碟上面一個檔案,同一個資料庫檔案可以在不同機器上面使用,最大支援資料庫到2T,字元和BLOB的支援僅限制於可用記憶體;
4. 整個系統少於3萬行程式碼,少於250KB的記憶體佔用(gcc),大部分應用比目前常見的客戶端/服務端的資料庫快,沒有其它依賴
5. 原始碼開放,程式碼95%有較好的註釋,簡單易用的API。官方帶有TCL的編譯版本。

2.3 Empress(商業資料庫)

開發階段特點:
1. 可嵌入程式,該特性使應用程式和資料庫工作於統一地址空間,增強了系統的穩定性,提高了系統的效率。
2. 確定的響應時間,Empress 可以使資料的響應時間相對一致,使用者可以設定一個超時限制,如果在規定時間內沒有完成插入,修改等操作,系統會報錯。
3. 快速的操作Empress 提供了核心級的CAPI,稱為MR, 用MR編寫的應用程式在執行時不需要解析。另外在MR中加速的機制還包括優秀的加鎖控制,記憶體管理和基於記錄數量的選擇功能。
4. 靈活的開發方式,Empress 提供多種開發介面,加快開發程式而無需開發者重新學習開發語言和熟悉開發環境。
5. 友好的儲存方式,Empress 資料庫可以放在作業系統支援的任何儲存裝置中,Empress的表單甚至可以分割放在不同的儲存裝置中,比如在記憶體,硬碟和CD-ROM中。
6. 微型核心結構 Empress 高度單元化, 可根據需要選擇需要的單元,從而縮小產品中Empress 資料庫所佔用的資源。
7. 寬廣的平臺支援,Empress 支援多種硬體平臺和軟體平臺, 也可移植到客戶要求的硬體平臺或作業系統。
技術優勢:
1. 微型核心結構,佔用少量記憶體空間,特別適合緊湊性的設計
2.一週7天,每天24小時連續工作,無需任何額外操作免維護
3. 核心級 CAPI 介面,使執行速度最大化
4. 高度靈活的SQL介面
5. 優秀的掉電恢復能力
6. 強壯的交易和鎖存機制
7. 支援SCSI,RAID,IDE,RAM,CD-RW,DVD-ROM,CF,等儲存介質
8. 支援Unicode 碼
9. 引擎可載入於磁碟和記憶體

2.4 eXtremeDB

eXtremeDB特點:
1. 記憶體資料庫eXtremeDB將資料以程式直接使用的格式儲存在主記憶體之中,不僅剔除了檔案I/O的開銷,也剔除了檔案系統資料庫所需的緩衝和Cache機制。其結果是每個交易一微秒甚至更少的極限速度,相比於類磁碟資料庫而言,速度成百上千倍地提高。作為記憶體資料庫,eXtremeDB不僅效能高,而且資料儲存的效率也非常高。為了提高效能並方便程式使用,資料在eXtremeDB中不做任何壓縮,100M的空間可以儲存高達70M以上的有效資料,這是其他資料庫所不可想象的。
2. 混合資料庫,eXtremeDB不僅可以建立完全執行在主記憶體的記憶體資料庫,更可以建立磁碟/記憶體混合介質的資料庫。在eXtremeDB,我們把這種建立在磁碟、記憶體或磁碟+記憶體的執行模式稱為eXtremeDB Fusion融合資料庫。eXtremeDB Fusion兼顧資料管理的實時性與安全性要求,是實時資料管理的臺階性進步。
3. 嵌入式資料庫,eXtremeDB核心以連結庫的形式包含在應用程式之中,其開銷只有50KB~130KB。無論在嵌入式系統還是在實時系統之中,eXtremeDB都天然地嵌入在應用程式之中,在終端使用者毫不知情的情況下工作。eXtremeDB的這種天然嵌入性對實時資料管理至關重要:各個程式都直接訪問eXtremeDB資料庫,避免了程式間通訊,從而剔除了程式間通訊的開銷和不確定性。同時, eXtremeDB獨特的資料格式方便程式直接使用的,剔除了資料複製及資料翻譯的開銷,縮短了應用程式的程式碼執行路徑。
4. 由應用定製的API,應用程式對eXtremeDB資料庫的操作介面是根據應用資料庫設計而自動產生,不僅提升了效能,也剔除了通用介面所必不可少的動態記憶體分配,從而提高了應用系統的可靠性。定製過程簡單方便,由高階語言定製eXtremeDB資料庫中的表格、欄位、資料型別、事件觸發、訪問方法等應用特徵,通過eXtremeDB預編譯器自動產生訪問該資料庫的C/C++ API介面。
5. 可預測的資料管理
eXtremeDB獨特的體系結構,保證了資料管理的可預測性。eXtremeDB不僅更快、更小,而且更確定。在80雙核CPU的伺服器上,eXtremeDB在1TB記憶體裡儲存15B條記錄;無論記錄數多少,eXtremeDB可以在八十分之一微秒的時間內提取一條記錄。

2.5 Firebird嵌入伺服器版

Firebird嵌入伺服器版(Embedded Server),從Interbase開源衍生出的Firebird,充滿了勃勃生機。雖然它的體積比前輩Interbase縮小了幾十倍,但功能並無閹割。為了體現Firebird短小精悍的特色,開發小組在增加了超級伺服器版本之後,又增加了嵌入版本,最新版本為2.0。
Firebird的嵌入版有如下特色:
1、資料庫檔案與Firebird網路版本完全相容,差別僅在於連線方式不同,可以實現零成本遷移。
2、資料庫檔案僅受作業系統的限制,且支援將一個資料庫分割成不同檔案,突破了作業系統最大檔案的限制,提高了IO吞吐量
3、完全支援SQL92標準,支援大部分SQL-99標準功能。
4、豐富的開發工具支援,絕大部分基於Interbase的元件,可以直接使用於Firebird。
5、支援事務儲存過程觸發器等關聯式資料庫的所有特性。
6、可自己編寫擴充套件函式(UDF)。

2.6 mSQL介紹

mSQL(mini SQL)是一個單使用者資料庫管理系統,個人使用免費,商業使用收費。由於它的短小精悍,使其開發的應用系統特別受到網際網路使用者青睞。mSQL(mini SQL)是一種小型的關聯式資料庫,效能不是太好,對SQL語言的支援也不夠完全,但在一些網路資料庫應用中是足夠了。由於mSQL較簡單,在執行簡單的SQL語句時速度比MySQL略快,而MySQL在執行緒和索引上下了功夫,執行復雜的SQL語句時比mSQL,PostgreSQL等都要快一些。最新版本是2005年5月8日釋出的3.7.MSQL的標誌是一個鹿,見下圖。 圖1mSQL LOGO標誌
mSQL的技術特點:安全性方面,mSQL通過ACL檔案設定各主機上各使用者的訪問許可權,預設是 全部可讀/寫。mSQL缺乏 ANSI SQL 的大多數特徵,它僅僅實現了一個最最少的API,沒有事務和參考完整性。mSQL與Lite(一種類似C的指令碼語言,與分發一起發行)緊密結合,可以得到一個稱為 W3-mSQL的一個網站整合包,它是JDBC、ODBC、Perl和PHP API.

2SQLite資料庫實驗編輯

3.1 學習資源

sqlite中文網站:程式設計介紹、開源論壇
sqlite官方網站:原始碼下載、Getting Started、C/C++參考、各種其他文件、開發者論壇等。

3.2 在Linux主機上安裝執行SQLite

下載sqlite-amalgamation-3.5.9.tar.gz檔案
上傳sqlite-amalgamation-3.5.9.tar.gz至mike目錄
解壓縮sqlite-amalgamation-3.5.9.tar.gz
tar zxvf sqlite-amalgamation-3.5.9.tar.gz
進入sqlite目錄:
cd sqlite-amalgamation-3.5.9
配置
./configure
編譯
make
安裝
make install
主要安裝瞭如下檔案:
/usr/local/include/sqlite3.h
/usr/local/include/sqlite3ex.h
/usr/local/lib/libsqlite3*
/usr/local/bin/sqlite3
其中標頭檔案用於提供sqlite提供的庫函式原型,在編寫資料庫應用程式時,會include這些標頭檔案;庫函式則是sqlite提供的動態連連結庫,如果應用程式使用了sqlite的庫函式,則編譯時會動態連結這些庫;bin目錄下的sqlite應用程式是一個shell程式,提供使用者命令列介面,使用者可以體驗sqlite提供的功能。
3.2.1 用主機命令列方式體驗sqlite的功能 參見附件1
3.2.2 編寫使用資料庫的應用程式 程式程式碼如下:
#include
#include
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i
printf("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
}
printf("\n");
return 0;
}
int main(int argc, char **argv){
sqlite3 *db;
char *zErrMsg = 0;
int rc;
if( argc!=3 ){
fprintf(stderr, "Usage: %s DATABASE SQL-STATEMENT\n", argv[0]);
exit(1);
}
rc = sqlite3_open(argv[1], &db);
if( rc ){
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
rc = sqlite3_exec(db, argv[2], callback, 0, &zErrMsg);
if( rc!=SQLITE_OK ){
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
}
sqlite3_close(db);
return 0;
}
(1) 建立sqlite_test.c檔案,內容如上。把sqlite_test.c檔案放到mike目錄下。
(2) 編譯sqlite_test.c檔案:
gcc -I /usr/local/include -L /usr/local/lib -o sqlite_test sqlite_test.c -lsqlite3
產生sqlite_test的應用程式。
Gcc命令列引數解釋:
-Ldir
制定編譯的時候,搜尋庫的路徑。比如你自己的庫,可以用它制定目錄,不然編譯器將只在標準庫的目錄找。這個dir就是目錄的名稱。
-llibrary
制定編譯的時候使用的庫
-Idir
在你是用#include"file"的時候,gcc/g++會先在當前目錄查詢你所制定的標頭檔案,如果沒有找到,他回到預設的標頭檔案目錄找,如果使用-I制定了目錄,他會先在你所制定的目錄查詢,然後再按常規的順序去找.
對於#include,gcc/g++會到-I制定的目錄查詢,查詢不到,然後將到系統的預設的標頭檔案目錄查詢
(3) 測試sqlite_test程式
test程式接受兩個引數:第一個引數為資料庫檔名,第二個引數為要執行的SQL語句。程式中與SQLite3的API相關的地方主要有四個:第27行的sqlite3_open(),第33行的sqlite3_exec(),第30行和第38行的sqlite3_close(),第36行的sqlite3_free()。關於SQLite3的API介面請參閱文獻[1]。
下面是測試test程式的完整過程,需要注意的是由於命令較長,因此每一個命令都分成了多行輸入,這樣看起來要清楚一些:
[root@Sitsang2 root]$./test xyz.db "create table
> tbl0(name varchar(10), number smallint);"
[root@Sitsang2 root]$./test xyz.db "insert into
> tbl0 values('cyc', 1);"
[root@Sitsang2 root]$./test xyz.db "insert into
> tbl0 values('dzy', 2);"
[root@Sitsang2 root]$./test xyz.db "select *
> from tbl0;"
name = cyc
number = 1
name = dzy
number = 2
解釋一下上面所用的測試命令:第一條命令在xyz.db這個資料庫檔案中建立了一個tbl0表,表中包含兩個欄位,欄位name是一個變長字串,欄位number的型別為smallint;第二條命令向資料庫的tbl0表中插入了一條記錄(‘cyc’,1);第三條命令向資料庫的tbl0表中插入了一條記錄(‘dzy’,2);第四條命令則是查詢表tbl0中的所有內容,與預期的一樣,這條命令列印除了資料庫中的兩條剛插入的記錄。由此可以得出結論,這幾條命令確實都已經按照預期的目標工作了。
同時,在向資料庫中插入上面所示的資料之後,可以看到資料庫檔案xyz.db大小已經發生了變化:
[root@Sitsang2 root]$ll -h xyz.db
-rw-r--r-- 1 root root 2.0k Jan 1 00:18 xyz.db
此時資料庫檔案xyz.db的大小為2K。測試結果表明資料庫能夠正常工作。

3.3 開發基於sqlite的嵌入式資料庫程式

3.3.1向開發板上移植SQLite 刪除先前的sqlite-amalgamation-3.5.9目錄
解壓縮sqlite-amalgamation-3.5.9.tar.gz
tar zxvf sqlite-amalgamation-3.5.9.tar.gz
進入sqlite目錄
cd sqlite-amalgamation-3.5.9
我們在/home/mike目錄下建立一個目錄sqlite_install,作為後面使用的安裝目錄
mkdir /home/mike/sqlite_install
配置原始檔,以生成適合於 開發板的Makefile
export PATH=/usr/local/arm-linux/bin:$PATH
./configure --host=arm-linux --prefix=/home/mike/sqlite_install
解釋:linux軟體的安裝方式一般有2種:rpm方式;利用./configure配置,然後make、再make install方式;請查閱相關資料。這裡,--host引數指名編譯出的軟體要在arm-linux平臺上執行;--prefix則指名軟體安裝目錄。Configure執行的結果是產生一個相應的Makefile檔案,該檔案遵從configure後面兩個引數的指定。
編譯
make
安裝
make install
進入/home/sqlite_install觀看,可以發現有三個目錄,分別是include、lib、bin。分別存放了編寫sqlite資料庫應用程式需要的標頭檔案、應用程式編譯和執行時均需要的庫函式、可在arm linux系統上執行的sqlite命令列程式。
3.3.2編寫、編譯使用SQLite函式庫的應用程式 我們利用交叉開發工具鏈對在3.2.2中編寫的程式sqlite_test.c從新編譯,使之可以在開發板上執行。
編譯源程式:
確認sqlite_test.c在mike目錄下,進入mike目錄
arm-linux-gcc -I ./sqlite_install/include/ -L ./ sqlite_install/lib/ -o sqlite_test sqlite_test.c -lsqlite3
執行完畢後,產生arm 版本的sqlite_test檔案
檢視sqlite_test檔案的屬性
file sqlite_test
把sqlite_test檔案複製到根檔案系統的某個目錄,把sqlite_test執行所需要的庫函式,即/home/mike/sqlite_install/lib目錄下的所有檔案複製到根檔案目錄的lib目錄下,重新制作cramfs或者yaffs格式的根檔案系統映像,並燒寫到實驗系統中。
系統啟動後,可仿照3.2中的方式對sqlite_test進行測試。附件1 sqlite3: A command-line access program for SQLite database The SQLite library includes a simple command-line utility named sqlite3that allows the user to manually enter and execute SQL commands against an SQLite database. This document provides a brief introduction on how to use sqlite3.
Getting Started
To start the sqlite3program, just type "sqlite3" followed by the name the file that holds the SQLite database. If the file does not exist, a new one is created automatically. The sqlite3program will then prompt you to enter SQL. Type in SQL statements (terminated by a semicolon), press "Enter" and the SQL will be executed.
For example, to create a new SQLite database named "ex1" with a single table named "tbl1", you might do this:
sqlite3 ex1
SQLite version 3.3.10
Enter ".help" for instructions
sqlite> create table tbl1(one varchar(10), two smallint);
sqlite> insert into tbl1 values('hello!',10);
sqlite> insert into tbl1 values('goodbye', 20);
sqlite> select * from tbl1;
hello!|10
goodbye|20
sqlite>
You can terminate the sqlite3 program by typing your systems End-Of-File character (usually a Control-D) or the interrupt character (usually a Control-C).
Make sure you type a semicolon at the end of each SQL command! The sqlite3 program looks for a semicolon to know when your SQL command is complete. If you omit the semicolon, sqlite3 will give you a continuation prompt and wait for you to enter more text to be added to the current SQL command. This feature allows you to enter SQL commands that span multiple lines. For example:
sqlite> CREATE TABLE tbl2 (
...> f1 varchar(30) primary key,
...> f2 text,
...> f3 real
...> );
sqlite>
Aside: Querying the SQLITE_MASTER table
The database schema in an SQLite database is stored in a special table named "sqlite_master". You can execute "SELECT" statements against the special sqlite_master table just like any other table in an SQLite database. For example:
sqlite3 ex1
SQlite vresion 3.3.10
Enter ".help" for instructions
sqlite> select * from sqlite_master;
type = table
name = tbl1
tbl_name = tbl1
rootpage = 3
sql = create table tbl1(one varchar(10), two smallint)
sqlite>
But you cannot execute DROP TABLE, UPDATE, INSERT or DELETE against the sqlite_master table. The sqlite_master table is updated automatically as you create or drop tables and indices from the database. You can not make manual changes to the sqlite_master table.
The schema for TEMPORARY tables is not stored in the "sqlite_master" table since TEMPORARY tables are not visible to applications other than the application that created the table. The schema for TEMPORARY tables is stored in another special table named "sqlite_temp_master". The "sqlite_temp_master" table is temporary itself.
Special commands to sqlite3
Most of the time, sqlite3 just reads lines of input and passes them on to the SQLite library for execution. But if an input line begins with a dot ("."), then that line is intercepted and interpreted by the sqlite3 program itself. These "dot commands" are typically used to change the output format of queries, or to execute certain prepackaged query statements.
For a listing of the available dot commands, you can enter ".help" at any time. For example:
sqlite> .help
.bail ON|OFF Stop after hitting an error. Default OFF
.databases List names and files of attached databases
.dump ?TABLE? ... Dump the database in an SQL text format
.echo ON|OFF Turn command echo on or off
.exit Exit this program
.explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
.header(s) ON|OFF Turn display of headers on or off
.help Show this message
.import FILE TABLE Import data from FILE into TABLE
.indices TABLE Show names of all indices on TABLE
.load FILE ?ENTRY? Load an extension library
.mode MODE ?TABLE? Set output mode where MODE is one of:
csv Comma-separated values
column Left-aligned columns. (See .width)
html HTML code
insert SQL insert statements for TABLE
line One value per line
list Values delimited by .separator string
tabs Tab-separated values
tcl TCL list elements
.nullvalue STRING Print STRING in place of NULL values
.output FILENAME Send output to FILENAME
.output stdout Send output to the screen
.prompt MAIN CONTINUE Replace the standard prompts
.quit Exit this program
.read FILENAME Execute SQL in FILENAME
.schema ?TABLE? Show the CREATE statements
.separator STRING Change separator used by output mode and .import
.show Show the current values for various settings
.tables ?PATTERN? List names of tables matching a LIKE pattern
.timeout MS Try opening locked tables for MS milliseconds
.width NUM NUM ... Set column widths for "column" mode
sqlite>
Changing Output Formats
The sqlite3 program is able to show the results of a query in eight different formats: "csv", "column", "html", "insert", "line", "tabs", and "tcl". You can use the ".mode" dot command to switch between these output formats.
The default output mode is "list". In list mode, each record of a query result is written on one line of output and each column within that record is separated by a specific separator string. The default separator is a pipe symbol ("|"). List mode is especially useful when you are going to send the output of a query to another program (such as AWK) for additional processing.
sqlite> .mode list
sqlite> select * from tbl1;
hello|10
goodbye|20
sqlite>
You can use the ".separator" dot command to change the separator for list mode. For example, to change the separator to a comma and a space, you could do this:
sqlite> .separator ", "
sqlite> select * from tbl1;
hello, 10
goodbye, 20
sqlite>
In "line" mode, each column in a row of the database is shown on a line by itself. Each line consists of the column name, an equal sign and the column data. Successive records are separated by a blank line. Here is an example of line mode output:
sqlite> .mode line
sqlite> select * from tbl1;
one = hello
two = 10
one = goodbye
two = 20
sqlite>
In column mode, each record is shown on a separate line with the data aligned in columns. For example:
sqlite> .mode column
sqlite> select * from tbl1;
one two
---------- ----------
hello 10
goodbye 20
sqlite>
By default, each column is at least 10 characters wide. Data that is too wide to fit in a column is truncated. You can adjust the column widths using the ".width" command. Like this:
sqlite> .width 12 6
sqlite> select * from tbl1;
one two
------------ ------
hello 10
goodbye 20
sqlite>
The ".width" command in the example above sets the width of the first column to 12 and the width of the second column to 6. All other column widths were unaltered. You can gives as many arguments to ".width" as necessary to specify the widths of as many columns as are in your query results.
If you specify a column a width of 0, then the column width is automatically adjusted to be the maximum of three numbers: 10, the width of the header, and the width of the first row of data. This makes the column width self-adjusting. The default width setting for every column is this auto-adjusting 0 value.
The column labels that appear on the first two lines of output can be turned on and off using the ".header" dot command. In the examples above, the column labels are on. To turn them off you could do this:
sqlite> .header off
sqlite> select * from tbl1;
hello 10
goodbye 20
sqlite>
Another useful output mode is "insert". In insert mode, the output is formatted to look like SQL INSERT statements. You can use insert mode to generate text that can later be used to input data into a different database.
When specifying insert mode, you have to give an extra argument which is the name of the table to be inserted into. For example:
sqlite> .mode insert new_table
sqlite> select * from tbl1;
INSERT INTO 'new_table' VALUES('hello',10);
INSERT INTO 'new_table' VALUES('goodbye',20);
sqlite>
The last output mode is "html". In this mode, sqlite3 writes the results of the query as an XHTML table. The beginning
and the ending
are not written, but all of the intervening s, s, and s are. The html output mode is envisioned as being useful for CGI.
Writing results to a file
By default, sqlite3 sends query results to standard output. You can change this using the ".output" command. Just put the name of an output file as an argument to the .output command and all subsequent query results will be written to that file. Use ".output stdout" to begin writing to standard output again. For example:
sqlite> .mode list
sqlite> .separator |
sqlite> .output test_file_1.txt
sqlite> select * from tbl1;
sqlite> .exit
cat test_file_1.txt
hello|10
goodbye|20
$
Querying the database schema
The sqlite3 program provides several convenience commands that are useful for looking at the schema of the database. There is nothing that these commands do that cannot be done by some other means. These commands are provided purely as a shortcut.
For example, to see a list of the tables in the database, you can enter ".tables".
sqlite> .tables
tbl1
tbl2
sqlite>
The ".tables" command is similar to setting list mode then executing the following query:
SELECT name FROM sqlite_master
WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
UNION ALL
SELECT name FROM sqlite_temp_master
WHERE type IN ('table','view')
ORDER BY 1
In fact, if you look at the source code to the sqlite3 program (found in the source tree in the file src/shell.c) you'll find exactly the above query.
The ".indices" command works in a similar way to list all of the indices for a particular table. The ".indices" command takes a single argument which is the name of the table for which the indices are desired. Last, but not least, is the ".schema" command. With no arguments, the ".schema" command shows the original CREATE TABLE and CREATE INDEX statements that were used to build the current database. If you give the name of a table to ".schema", it shows the original CREATE statement used to make that table and all if its indices. We have:
sqlite> .schema
create table tbl1(one varchar(10), two smallint)
CREATE TABLE tbl2 (
f1 varchar(30) primary key,
f2 text,
f3 real
)
sqlite> .schema tbl2
CREATE TABLE tbl2 (
f1 varchar(30) primary key,
f2 text,
f3 real
)
sqlite>
The ".schema" command accomplishes the same thing as setting list mode, then entering the following query:
SELECT sql FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE type!='meta'
ORDER BY tbl_name, type DESC, name
Or, if you give an argument to ".schema" because you only want the schema for a single table, the query looks like this:
SELECT sql FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'
ORDER BY substr(type,2,1), name
You can supply an argument to the .schema command. If you do, the query looks like this:
SELECT sql FROM
(SELECT * FROM sqlite_master UNION ALL
SELECT * FROM sqlite_temp_master)
WHERE tbl_name LIKE '%s'
AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'
ORDER BY substr(type,2,1), name
The "%s" in the query is replace by your argument. This allows you to view the schema for some subset of the database.
sqlite> .schema %abc%
Along these same lines, the ".table" command also accepts a pattern as its first argument. If you give an argument to the .table command, a "%" is both appended and prepended and a LIKE clause is added to the query. This allows you to list only those tables that match a particular pattern.
The ".databases" command shows a list of all databases open in the current connection. There will always be at least 2. The first one is "main", the original database opened. The second is "temp", the database used for temporary tables. There may be additional databases listed for databases attached using the ATTACH statement. The first output column is the name the database is attached with, and the second column is the filename of the external file.
sqlite> .databases
Converting An Entire Database To An ASCII Text File
Use the ".dump" command to convert the entire contents of a database into a single ASCII text file. This file can be converted back into a database by piping it back into sqlite3.
A good way to make an archival copy of a database is this:
echo '.dump' | sqlite3 ex1 | gzip -c >ex1.dump.gz
This generates a file named ex1.dump.gzthat contains everything you need to reconstruct the database at a later time, or on another machine. To reconstruct the database, just type:
zcat ex1.dump.gz | sqlite3 ex2
The text format is pure SQL so you can also use the .dump command to export an SQLite database into other popular SQL database engines. Like this:
createdb ex2
sqlite3 ex1 .dump | psql ex2
Other Dot Commands
The ".explain" dot command can be used to set the output mode to "column" and to set the column widths to values that are reasonable for looking at the output of an EXPLAIN command. The EXPLAIN command is an SQLite-specific SQL extension that is useful for debugging. If any regular SQL is prefaced by EXPLAIN, then the SQL command is parsed and analyzed but is not executed. Instead, the sequence of virtual machine instructions that would have been used to execute the SQL command are returned like a query result. For example:
sqlite> .explain
sqlite> explain delete from tbl1 where two<20;
addr opcode p1 p2 p3
---- ------------ ----- ----- -------------------------------------
0 ListOpen 0 0
1 Open 0 1 tbl1
2 Next 0 9
3 Field 0 1
4 Integer 20 0
5 Ge 0 2
6 Key 0 0
7 ListWrite 0 0
8 Goto 0 2
Noop 0 0
10 ListRewind 0 0
11 ListRead 0 14
12 Delete 0 0
13 Goto 0 11
14 ListClose 0 0
The ".timeout" command sets the amount of time that the sqlite3program will wait for locks to clear on files it is trying to access before returning an error. The default value of the timeout is zero so that an error is returned immediately if any needed database table or index is locked.
And finally, we mention the ".exit" command which causes the sqlite3 program to exit.

3.4 Berkeley DB

技術特點:
1. Berkeley DB是一個開放原始碼的內嵌式資料庫管理系統,能夠為應用程式提供高效能的資料管理服務。應用它程式設計師只需要呼叫一些簡單的API就可以完成對資料的訪問和管理。(不使用SQL語言)
2. Berkeley DB為許多程式語言提供了實用的API介面,包括C、C++、Java、Perl、Tcl、Python和PHP等。所有同資料庫相關的操作都由Berkeley DB函式庫負責統一完成。
3. Berkeley DB輕便靈活(Portable),可以執行於幾乎所有的UNIX和Linux系統及其變種系統、Windows作業系統以及多種嵌入式實時作業系統之下。Berkeley DB被連結到應用程式中,終端使用者一般根本感覺不到有一個資料庫系統存在。
4. Berkeley DB是可伸縮(Scalable)的,這一點表現在很多方面。Database library本身是很精簡的(少於300KB的文字空間),但它能夠管理規模高達256TB的資料庫。它支援高併發度,成千上萬個使用者可同時操縱同一個資料庫。Berkeley DB能以足夠小的空間佔用量執行於有嚴格約束的嵌入式系統
Berkeley DB在嵌入式應用中比關聯式資料庫和物件導向資料庫要好,有以下兩點原因:
(1)因為資料庫程式庫同應用程式在相同的地址空間中執行,所以資料庫操作不需要程式間的通訊。在一臺機器的不同程式間或在網路中不同機器間進行程式通訊所花費的開銷,要遠遠大於函式呼叫的開銷;
(2)因為Berkeley DB對所有操作都使用一組API介面,因此不需要對某種查詢語言進行解析,也不用生成執行計劃,大大提高了執行效。

3國產嵌入式資料庫OpenBASE Lite編輯

OpenBASE Lite是東軟集團股份有限公司開發的嵌入式資料庫產品。它是一個典型的輕量級資料庫,定製的資料庫引擎大小在250KB到600KB之間伸縮,可支援多種桌面作業系統、主流嵌入式系統平臺及不同的處理器。作為一款功能全面的關係型資料庫系統,OpenBASE Lite支援標準的SQL語法、ACID事務特性、備份/恢復等功能,提供了標準化開發介面JDBC、ODBC,能夠在嵌入式環境下沿用關聯式資料庫的經驗繼續來進行應用的開發。OpenBASE Lite提供了記憶體資料庫執行模式,提供高速的資料訪問與更新能力。
產品特色
1.完善的資料管理功能
OpenBASE Lite嵌入式資料庫具有完善的資料管理功能,提供了對SQL92標準子集的支援;提供對標準資料型別以及BLOB/CLOB型別的支援;支援資料庫完整性控制;具有完整的資料管理能力,可以處理GB級的資料量;並提供對空間資料的管理能力。
2.廣泛的平臺通用性
OpenBASE Lite嵌入式資料庫可執行於Windows 2000/2003/XP/Vista/Win 7、Windows Mobile 5&6、Windows CE、Linux、Embedded Linux、VxWorks、Symbian、Android等多種作業系統平臺。
3.微小的核心核心
OpenBASE Lite嵌入式資料庫具有微核心特性,可根據需求定製和裁剪,核心大小在250KB~600KB之間伸縮。
4.真正的零管理
在OpenBASE Lite嵌入式資料庫的使用過程中無需對資料庫進行配置,在移動終端應用中實現了“零管理”。
5.出色的處理效能
OpenBASE Lite嵌入式資料庫可以作為記憶體資料庫進行使用,實現了高速的資料訪問與更新,單條資料處理時間不超過15us;在併發處理效能上,提供庫級鎖與表級鎖併發訪問控制,提高了程式與多執行緒對資料庫併發訪問處理效能。
6.充分的安全保障
OpenBASE Lite嵌入式資料庫支援使用者身份認證以及資料庫物件自主訪問控制,可以有效防止使用者資料的非法訪問;支援128-bit AES儲存加密,以保證資料庫檔案安全性。
7.快速的故障恢復
提供了日誌和故障恢復機制有效地保障了事務的ACID特性,另外還提供了資料庫的聯機熱備與主從複製功能,使使用者可以簡單快速地提高應用系統的可靠性。
8.標準的訪問介面
OpenBASE Lite嵌入式資料庫為嵌入式應用的開發提供了C API介面以及JDBC/ODBC標準訪問介面。
9.豐富的實用工具
OpenBASE Lite提供了豐富的實用工具進行資料庫管理,包括資料庫圖形管理工具、建庫工具、查詢工具、備份恢復工具、匯入匯出工具及資料庫加解密工具等。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28371090/viewspace-1141199/,如需轉載,請註明出處,否則將追究法律責任。

什麼叫嵌入式資料庫?
請登入後發表評論 登入
全部評論

相關文章