An Introduction to sqlite3(sqlite3简介)
0.本文是对sqlite官方网站中的文档以及介绍进行的简要翻译,主要介绍了sqlite的一些Features以及其常见的应用。
1.sqlite简介
sqlite是实现了以下特征的软件:
i.Self-Contained :sqlite最大程度的实现了self-contained。它实现了对外部程序库以及操作系统的最低要求,这使得它非常适合应用于嵌入式设备,同时,可以应用于一些稳定的,很少修改配置的应用程序中。
sqlite是使用ANSI-C开发的,可以被任何的标准C编译器来进行编译。
sqlite与操作系统和存储设备(磁盘)之间的通信是通过VFS(Virtual File System)来完成的,对于嵌入式设备来说,开发一个可用的VFS并不困难。
sqlite使用互斥来保证多线程环境下的操作安全性。
sqlite的源代码是一个单一的C文件-sqlite3.c,如果project需要使用它,只需要包含这个C文件和对应的头文件(sqlite3.h)。
ii.Serverless :大多数sql数据库引擎都作为一个独立的服务器进程,应用程序通过使用一 些协议,比如TCP/IP来发送请求给服务器,并接受结果,以这种方式来与数据库服务器进行通信。sqlite与此不同,进程可以通过访问数据库直接进行 数据库文件的读写而不需要中间层的服务器进程。这样的实现的主要的好处是不需要进行安装,配置,初始化,管理以及维护单独的服务进程。但是,数据库引擎可 以通过使用服务器来预防客户端应用程序的bug,确保服务器不被客户端的错误所损坏。大多数sql数据库都是基于C/S模式的,在serverless的 数据库中,sqlite是目前唯一允许多个应用同时访问的。
iii.Zero-Configuration :sqlite不需要任何配置---install、setup、configure server、administration、create instance、assign permissions、recover、troubleshooting。
iv.Transactional :sqlite实现了ACID(Atomic-原子性,Consistent-一致性,Isolated-隔离性,Durable-持久性)。sqlite实现了序列化事务来保证ACID,即使发生程序异常,操作系统异常或者电源故障。
v.Most Widely Deployed sql Database :大多数数据库是C/S模式 的,而一台服务器可以为很多人提供服务,而sqlite是嵌入式的数据库软件,大多数用户会同时使用多个数据库的拷贝。sqlite大量的被用于手 机,PDA,MP3播放器以及机顶盒设备。下面列举了一些使用sqlite作为嵌入式服务器的应用:
Mozilla Firefox使用sqlite作为数据库。
Mac计算机中的包含了多份sqlite的拷贝,用于不同的应用。
PHP将sqlite作为内置的数据库。
Skype客户端软件在内部使用sqlite。
SymbianOS(智能手机操作平台的领航)内置sqlite。
AOL邮件客户端绑定了sqlite。
Solaris 10在启动过程中需要使用sqlite。
McAfee杀毒软件使用sqlite。
iPhones使用sqlite。
Symbian和Apple以外的很多手机生产厂商使用sqlite。
关于使用sqlite的Famous Users,请参考http://www.sqlite.org/famous.html
vi.OpenSource :sqlite处于public domain中,官方声明如下:
Anyone is free to copy,modify,publish,use,compile,sell,or distribute the original sqlite code,either in source code form or as a compiled binary,for any purpose,commercial or non-commercial,and by any means.
一些其他的特色例如执行速度比C/S模式的数据库引擎快、简单易用、源代码注释良好、跨平台等请参考sqlite Features列表:
http://www.sqlite.org/features.html
2.sqlite的应用场景
sqlite具有小巧,快速和可靠的优点是源于其简单-管理简单,操作简单,嵌入简单以及维护自定义简单。
当 然,sqlite也有一些缺点,这取决于用户的需求,sqlite不具有高度并发性,良好的存取权限控制,没有内置的函数集,不支持存储过程以及深奥的 sql语言特性(sqlite并不完全支持sql92),不支持XML/Java扩展,不具有tera-byte或者peta-byte的可伸缩性等等。 所以,当需要上面列举的这些功能时,sqlite就不在适合用户了。sqlite不是企业级的RDBMS。
下面列举了一些应用场景:
应用文件格式
嵌入式设备和应用
中小型网站
内置或者临时数据库
命令行数据集分析工具
作为企业数据库的替代产品--demo阶段或者测试阶段
教学
如果为了学习它,以备将来使用,或者要开发类似的应用场景,可以使用sqlite作为数据库软件。
企业级的RDBMS适用于C/S应用,高吞吐量网站,非常大的数据集以及高度并发的应用。
关于sqlite区别于其他数据库的特有属性,请参考:http://www.sqlite.org/different.html
3.下载与使用
根据不同的平台,可以在下载页面:http://www.sqlite.org/download.html 下载对应的发布版本。笔者接下来将介绍sqlite在windows上的使用。多种编程语言可以通过使用wrapper或者driver来访问sqlite,下面将介绍使用java编程语言来访问sqlite并做一些常见的操作。
i.下载sqlite
windows版本的sqlite发布包解压缩之后只是一个二进制的sqlite3.exe文件。
可以直接运行该程序,即可进入sqlite3控制台(类似于MysqL的控制台),可以通过输入.help来获取帮助信息。.quit可以推出控制台。控制台的一个简单操作如下:
//可以直接使用sqlite3 databaseName来创建数据库
//sqlite不支持create database,drop database这样的语句,drop database时直接,删除数据库文件即可。
- E:/tools/sqlite>sqlite3.exetest
- sqliteversion3.6.11
- Enter".help" for instructions
- Entersqlstatementsterminatedwitha";"
- sqlite>.help
- .backup?DB?FILEBackupDB(default "main" )toFILE
- .bailON|OFFStopafterhittinganerror.DefaultOFF
- .databasesListnamesandfilesofattacheddatabases
- //lotsofotherhelpinformationomittedhere
- .widthNUMNUM...Setcolumnwidthsfor "column" mode
- sqlite>.databases
- seqnamefile
- ----------------------------------------------------------------------------
- 0mainE:/tools/sqlite/test
- sqlite>createtablestudent(idvarchar(10),namevarchar(20),agesmallint);
- sqlite>select*fromstudent;
- sqlite>insertintostudentvalues('1001' , 'lovesizhao' ,26);
- sqlite>select*fromstudent;
- 1001|lovesizhao|26
- sqlite>droptablestudent;
- sqlite>.quit