嵌入式数据库(2023最新嵌入式数据库百科介绍)

由网友(颜值爆表)分享简介:嵌进式数据库的名称去自其奇特的运转模式。这类数据库嵌进到了使用步伐入程中,消弭了取客户机办事器配置相干的开支。嵌进式数据库现实上是轻质级的,正在运转时,它们需求较长的内存。它们是应用精简代码编写的,对于于嵌进式设备,其速率更快,效验更抱负。嵌进式运转模式许可嵌进式数据库经由过程 SQL 去轻快办理使用步伐数据,而没有寄托本初的文...

嵌入式数据库的名称来自其独特的运行模式。这种数据库嵌入到了应用程序进程中,消除了与客户机服务器配置相关的开销。嵌入式数据库实际上是轻量级的,在运行时,它们需要较少的内存。它们是使用精简代码编写的,对于嵌入式设备,其速度更快,效果更理想。嵌入式运行模式允许嵌入式数据库通过 SQL 来轻松管理应用程序数据,而不依靠原始的文本文件。嵌入式数据库还提供零配置运行模式,这样可以启用其中一个并运行一个快照。

中文名称

嵌入式数据库

名称来源

来自其独特的运行模式

领域

计算机

外文名称

ERTDBS

简介

具有定时限制的特性

嵌入式数据库介绍

基于嵌入式数据库的矿场安全系统设计方案详解

在嵌入式系统中,对数据库的操作具有定时限制的特性,这里把应用于嵌入式系统的数据库系统称为嵌入式数据库系统或嵌入式实时数据库系统(ERTDBS)。

可靠性要求是毋庸置疑的,嵌入式系统必须能够在没有人工干预的情况下,长时间不间断地运行。同时要求数据库操作具备可预知性,而且系统的大小和性能也都必须是可预知的,这样才能保证系统的性能。嵌入式系统中会不可避免地与底层硬件打交道,因此在数据管理时,也要有底层控制的能力,如什么时候会发生磁盘操作,磁盘操作的次数,如何控制等。底层控制的能力是决定数据库管理操作的关键。

目前嵌入式软件系统开发的挑战之一,体现在对各种数据的管理能否建立一套可靠、高效、稳定的管理模式,嵌入式数据库可谓应运而生。

嵌入式数据库是嵌入式系统的重要组成部分,也成为对越来越多的个性化应用开发和管理而采用的一种必不可少的有效手段。

嵌入式数据库用途广泛,如用于消费电子产品、移动计算设备、企业实时管理应用、网络存储与管理以及各种专用设备,这一市场目前正处于高速增长之中。 举简单例子,手机原来只用来打电话、发短信,现在手机增加了很多新的功能,比如彩信、音乐、摄影、视频等等,应用的功能多了,系统就变得复杂。

常用的嵌入式数据库的比较

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,我们把这种建立在磁盘、内存或磁盘+内存的运行模式称为eXtremeDBFusion融合数据库。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

#include

static int callback(void *NotUsed, int argc, char **argv, char **azColName){

int i;

for(i=0; i

printf("%s = %s", azColName[i], argv[i] ? argv[i] : "NULL");

}

printf("");

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", argv[0]);

exit(1);

}

rc = sqlite3_open(argv[1], &db);

if( rc ){

fprintf(stderr, "Can't open database: %s", 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", 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。测试结果表明数据库能够正常工作。

开发基于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 namedsqlite3that allows the user to manually enter and execute SQL commands against an SQLite database. This document provides a brief introduction on how to usesqlite3.

Getting Started

To start thesqlite3program, 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. Thesqlite3program 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 intosqlite3.

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 namedex1.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

9Noop0 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 thesqlite3program 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

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支持平台
BerkeleyDBrelational, objects, key-value pairs, documentsAGPL 3.0Android, iOS
Couchbase LitedocumentsApache 2.0Android, iOS
LevelDBkey-value pairsNew BSDAndroid, iOS
SQLiterelationalPublic DomainAndroid, iOS, Windows Phone, Blackberry
UnQLitekey-value pairs, documentsBSD 2-ClauseAndroid, iOS, Windows Phone

阅读全文

相关推荐

最新文章