複製鏈接
請複製以下鏈接發送給好友

嵌入式數據庫

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

嵌入式數據庫嵌入式數據庫介紹

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

嵌入式數據庫常用的嵌入式數據庫的比較

嵌入式數據庫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版本。

嵌入式數據庫SQLite

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

嵌入式數據庫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. 引擎可加載於磁盤和內存

嵌入式數據庫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可以在八十分之一微秒的時間內提取一條記錄。

嵌入式數據庫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)。

嵌入式數據庫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.

嵌入式數據庫SQLite數據庫實驗

嵌入式數據庫學習資源

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

嵌入式數據庫在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 <stdio.h>
#include <sqlite3.h>
static int callback(void *NotUsed, int argc, char **argv, char **azColName){
int i;
for(i=0; i<argc; 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<file>,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。測試結果表明數據庫能夠正常工作。

嵌入式數據庫開發基於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 <table> 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 <TABLE> and the ending </TABLE> are not written, but all of the intervening <TR>s, <TH>s, and <TD>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.

嵌入式數據庫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接口,因此不需要對某種查詢語言進行解析,也不用生成執行計劃,大大提高了運行效。

嵌入式數據庫國產嵌入式數據庫

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提供了豐富的實用工具進行數據庫管理,包括數據庫圖形管理工具、建庫工具、查詢工具、備份恢復工具、導入導出工具及數據庫加解密工具等。

嵌入式數據庫可用於移動開發的嵌入式數據庫簡介

嵌入式數據庫是輕量級的,獨立的庫,沒有服務器組件,無需管理,一個小的代碼尺寸,以及有限的資源需求。目前有幾種嵌入式數據庫,你可以在移動應用程序中使用。讓我們來看看這些最流行的數據庫。 [1] 
數據庫
數據類型存儲
License
支持平台
BerkeleyDB
relational, objects, key-value pairs, documents
AGPL 3.0
Android, iOS
Couchbase Lite
documents
Apache 2.0
Android, iOS
LevelDB
key-value pairs
New BSD
Android, iOS
SQLite
relational
Public Domain
Android, iOS, Windows Phone, Blackberry
UnQLite
key-value pairs, documents
BSD 2-Clause
Android, iOS, Windows Phone
參考資料