SQLite3简介(An Introduction to SQLite3)

前端之家收集整理的这篇文章主要介绍了SQLite3简介(An Introduction to SQLite3)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

An Introduction to sqlite3(sqlite3简介)
0.本文是对sqlite官方网站中的文档以及介绍进行的简要翻译,主要介绍了sqlite的一些Features以及其常见的应用。



1.sqlite简介

sqlite是实现了以下特征的软件:
i.Self-Containedsqlite最大程度的实现了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-Configurationsqlite不需要任何配置---install、setup、configure server、administration、create instance、assign permissions、recover、troubleshooting。


iv.Transactionalsqlite实现了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的拷贝,用于不同的应用。
PHPsqlite作为内置的数据库
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.OpenSourcesqlite处于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时直接,删除数据库文件即可。

@H_301_89@
  1. E:/tools/sqlite>sqlite3.exetest
  2. sqliteversion3.6.11
  3. Enter".help" for instructions
  4. Entersqlstatementsterminatedwitha";"
  5. sqlite>.help
  6. .backup?DB?FILEBackupDB(default "main" )toFILE
  7. .bailON|OFFStopafterhittinganerror.DefaultOFF
  8. .databasesListnamesandfilesofattacheddatabases
  9. //lotsofotherhelpinformationomittedhere
  10. .widthNUMNUM...Setcolumnwidthsfor "column" mode
  11. sqlite>.databases
  12. seqnamefile
  13. ----------------------------------------------------------------------------
  14. 0mainE:/tools/sqlite/test
  15. sqlite>createtablestudent(idvarchar(10),namevarchar(20),agesmallint);
  16. sqlite>select*fromstudent;
  17. sqlite>insertintostudentvalues('1001' , 'lovesizhao' ,26);
  18. sqlite>select*fromstudent;
  19. 1001|lovesizhao|26
  20. sqlite>droptablestudent;
  21. sqlite>.quit


这种直接在命令行提示符下面使用sqlite3的方式不是本文介绍的重点。
关于sqlite3支持sql语法相关内容如下:
sqlite3关键字:http://www.sqlite.org/lang_keywords.html
sqlite3支持sql语法:http://www.sqlite.org/lang.html
sqlite3不支持sql语法:http://www.sqlite.org/cvstrac/wiki?p=UnsupportedSql


ii.下载Java JDBC Driver for sqlite3
在下载页面http://www.sqlite.org/cvstrac/wiki?p=SqliteWrappers ,选择下载JDBC Driver for sqlite3。
笔者使用的是sqliteJDBC(http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC )。
该JDBC Driver的License是Apache Software Foundation 2.0 License。


iii.使用sqlite3数据库
笔者使用了NetBeans作为开发环境,写了简单的Java类,注:笔者并非要讲解sqlite3的所有功能以及细节的Features,只是作为一个介绍,如果有兴趣的话,请更多的参考sqlite3官方网站及相关资源。
NetBeans的使用不属于本文的介绍范围。下面介绍两个简单的Java类。
笔者使用的sqliteJDBC Driver版本为3.6.11。

sqlite3Util.java

@H_301_89@
  1. package sqlite3;
  2. import java.sql.Statement;
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.sqlException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public final class sqlite3Util{
  10. private final static StringURL= "jdbc:sqlite:" ;
  11. private final static StringTEST= "test" ;
  12. private static ConnectiongetConnection(Stringurl) throws sqlException{
  13. Connectionconn=null ;
  14. try {
  15. Class.forName("org.sqlite.JDBC" );
  16. conn=DriverManager.getConnection(url);
  17. }catch (ClassNotFoundExceptionex){
  18. Logger.getLogger(sqlite3Util.class .getName()).log(Level.SEVERE, null ,ex);
  19. }finally {
  20. return conn;
  21. }
  22. }
  23. public static Connectiongetsqlite3Connection() throws sqlException{
  24. return getConnection(URL+TEST);
  25. }
  26. public static Connectiongetsqlite3Connection(Stringdatabase) throws sqlException{
  27. //Heredatabasecanbedatabasename,databasefilenameormemory:
  28. //Connectionconnection=DriverManager.getConnection("jdbc:sqlite:C:/work/mydatabase.db");
  29. //Connectionconnection=DriverManager.getConnection("jdbc:sqlite:/home/leo/work/mydatabase.db");
  30. //Connectionconnection=DriverManager.getConnection("jdbc:sqlite::memory:");
  31. return getConnection(URL+((database== null ||database.equals( "" ))?TEST:database));
  32. }
  33. public static void close(Connectionconn,Statementstmt,ResultSetrs) throws sqlException{
  34. if (conn!= null ){
  35. conn.close();
  36. }
  37. if (stmt!= null ){
  38. stmt.close();
  39. }
  40. if (rs!= null ){
  41. rs.close();
  42. }
  43. }
  44. public static void closeQuiet(Connectionconn,ResultSetrs){
  45. try {
  46. if (conn!= null ){
  47. conn.close();
  48. }
  49. }catch (sqlExceptione){
  50. Logger.getLogger(sqlite3Util.class .getName()).log(Level.WARNING,e);
  51. }
  52. try {
  53. if (stmt!= null ){
  54. stmt.close();
  55. }
  56. }catch (sqlExceptione){
  57. Logger.getLogger(sqlite3Util.class .getName()).log(Level.WARNING,e);
  58. }
  59. try {
  60. if (rs!= null ){
  61. rs.close();
  62. }
  63. }catch (sqlExceptione){
  64. Logger.getLogger(sqlite3Util.class .getName()).log(Level.WARNING,e);
  65. }
  66. }
  67. }


sqlite3Test.java

@H_301_89@
  1. package sqlite3;
  2. import java.sql.Statement;
  3. import java.sql.Connection;
  4. import java.sql.DatabaseMetaData;
  5. import java.sql.ResultSet;
  6. import java.sql.sqlException;
  7. import java.util.logging.Level;
  8. import java.util.logging.Logger;
  9. public class sqlite3Test{
  10. /**
  11. *@paramargsthecommandlinearguments
  12. */
  13. public static void main(String[]args){
  14. //declareandinitializedatabasenamesandjava.sqlobjects
  15. Connectionconn=null ;
  16. Statementstmt=null ;
  17. ResultSetrs=null ;
  18. StringstudentDB="D:/NetBeans/NetbeansProjects/sqlite3/student.db" ;
  19. StringinmemoryDB=":memory:" ;
  20. //thedbmdisusedtocheckwhatsqlite3JDBCdriverprovideforJDBC
  21. DatabaseMetaDatadbmd=null ;
  22. try {
  23. //getconnectiontodefaultdatabase:test
  24. conn=sqlite3Util.getsqlite3Connection();
  25. if (conn== null ){
  26. return ;
  27. }
  28. dbmd=conn.getMetaData();
  29. //justprintinformationtostandardconsoleinsteadofoutputfile
  30. System.out.println("DatabaseProductName:" +dbmd.getDatabaseProductName());
  31. System.out.println("sqlKeywords:" +dbmd.getsqlKeywords());
  32. System.out.println("JDBCMajorVersion:" +dbmd.getJDBCMajorVersion());
  33. System.out.println("JDBCMinorVersion:" +dbmd.getJDBCMinorVersion());
  34. //getconnectiontodatabase:D:/NetBeans/NetbeansProjects/sqlite3/student.db
  35. conn=sqlite3Util.getsqlite3Connection(studentDB);
  36. stmt=conn.createStatement();
  37. stmt.executeUpdate("droptableifexistsstudent.student" );
  38. stmt.executeUpdate("createtablestudent(idsmallintprimarykey,namevarchar(20))" );
  39. stmt.executeUpdate("insertintostudentvalues(1,'zhangzhongliang')" );
  40. stmt.executeUpdate("insertintostudentvalues(2,'liusizhao')" );
  41. rs=stmt.executeQuery("select*fromstudent" );
  42. int i= 1 ;
  43. while (rs.next()){
  44. System.out.println("rowno=" +i);
  45. System.out.println("id=" +rs.getInt( "id" ));
  46. System.out.println("name=" +rs.getString( "name" ));
  47. i++;
  48. }
  49. stmt.executeUpdate("droptablestudent" );
  50. //getconnectiontodatabaseinmemory
  51. //thiswillnotcreateadatabasedatafileinyourdisk
  52. conn=sqlite3Util.getsqlite3Connection(inmemoryDB);
  53. stmt=conn.createStatement();
  54. stmt.executeUpdate("droptableifexistsstudent.student" );
  55. stmt.executeUpdate("createtablestudent(idsmallintprimarykey,'liusizhao')" );
  56. rs=stmt.executeQuery("select*fromstudent" );
  57. i=1 ;
  58. while (rs.next()){
  59. System.out.println("rowno=" +i);
  60. System.out.println("id=" +rs.getInt( "id" ));
  61. System.out.println("name=" +rs.getString( "name" ));
  62. i++;
  63. }
  64. stmt.executeUpdate("droptablestudent" );
  65. }catch (sqlExceptionex){
  66. Logger.getLogger(sqlite3Test.class .getName()).log(Level.SEVERE,ex);
  67. }finally {
  68. sqlite3Util.closeQuiet(conn,stmt,rs);
  69. }
  70. }
  71. }


4.小结
本文主要介绍了sqlite3作为数据库软件,与其他数据库不同的特性,优缺点以及应用场景的选择。最后通过使用JDBC Driver进行了一个简单的程序测试,介绍了如何通过JDBC来访问sqlite数据库


5.参考资料
sqlite官方地址:http://sqlite.org/
sqlite文档:http://sqlite.org/docs.html
sqlite JDBC Driver:http://www.xerial.org/trac/Xerial/wiki/SQLiteJDBC
Java JDBC:http://java.sun.com/products/jdbc/overview.html

原文链接:https://www.f2er.com/sqlite/203111.html

猜你在找的Sqlite相关文章