共找到2條詞條名為嵌入式資料庫的結果 展開

嵌入式資料庫

嵌入式資料庫

徠嵌入式資料庫的名稱來自其獨特的運行模式。這種資料庫嵌入到了應用程序進程中,消除了與客戶機伺服器配置相關的開銷。嵌入式資料庫實際上是輕量級的,在運行時,它們需要較少的內存。它們是使用精簡代碼編寫的,對於嵌入式設備,其速度更快,效果更理想。嵌入式運行模式允許嵌入式資料庫通過 SQL 來輕鬆管理應用程序數據,而不依靠原始的文本文件。嵌入式資料庫還提供零配置運行模式,這樣可以啟用其中一個並運行一個快照。

正文


1. 嵌入式資料庫介紹
在嵌入式系統中,對資料庫的操作具有定時限制的特性,這裡把應用於嵌入式系統的資料庫系統稱為嵌入式資料庫系統或嵌入式實時資料庫系統(ERTDBS)。
可靠性要求是毋庸置疑的,嵌入式系統必須能夠在沒有人工干預的情況下,長時間不間斷地運行。同時要求資料庫操作具備可預知性,而且系統的大小和性能也都必須是可預知的,這樣才能保證系統的性能。嵌入式系統中會不可避免地與底層硬體打交道,因此在數據管理時,也要有底層控制的能力,如什麼時候會發生磁碟操作,磁碟操作的次數,如何控制等。底層控制的能力是決定資料庫管理操作的關鍵。
目前嵌入式軟體系統開發的挑戰之一,體現在對各種數據的管理能否建立一套可靠、高效、穩定的管理模式,嵌入式資料庫可謂應運而生。
嵌入式資料庫是嵌入式系統的重要組成部分,也成為對越來越多的個性化應用開發和管理而採用的一種必不可少的有效手段。
嵌入式資料庫用途廣泛,如用於消費電子產品、移動計算設備、企業實時管理應用、網路存儲與管理以及各種專用設備,這一市場目前正處於高速增長之中。舉簡單例子,手機原來只用來打電話、發簡訊,現在手機增加了很多新的功能,比如彩信、音樂、攝影、視頻等等,應用的功能多了,系統就變得複雜。

2. 常用的嵌入式資料庫的比較


2.1 Progre
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.

3. SQLite資料庫實驗


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
9 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介面,因此不需要對某種查詢語言進行解析,也不用生成執行計劃,大大提高了運行效。

4 國產嵌入式資料庫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提供了豐富的實用工具進行資料庫管理,包括資料庫圖形管理工具、建庫工具、查詢工具、備份恢復工具、導入導出工具及資料庫加解密工具等。
  • 目錄