System.Data.SQLite数据库介绍

简介:
SQLite介绍
在介绍System.Data.SQLite之前需要介绍一下SQLite,SQLite是一个类似于Access的单机版数据库管理系统,它将所有数据库的定义(包括定义、表、索引和数据本身)都保存在一个单一的文件中。并且,SQLite是一个用C实现的类库,它在内存消耗、文件体积、简单性方面都有不错的表现,如果数据在10W条以下,查询速度也是相当快的。
SQLite具有以下特征:
实现多数SQL92的标准,包括事务(原子性、一致性、隔离性和持久性)、触发器和大多数的复杂查询。
不对插入或者更新的数据进行类型检查,你可以将字符串插入到整数列中(这个可能让有些用户不太适应)。
支持Windows/Linux/Unix等主流系统,还支持嵌入式系统如Android或Windows Mobile。
System.Data.SQLite
System.Data.SQLite是SQLite的加强版,它可以无需.NET Framework支持,由于它内部包含了一个ADO.NET 2.0引擎,所以.NET开发人员可以利用System.Data.SQLite方便地开发.NET程序。
System.Data.SQLite及SQLite也有一些限制,比如不支持行级及表级锁,当一个连接锁定数据库以用于写入数据,其它的数据库连接只能等待那个连接操作完成之后进行读写操作,SQLite.NET尝试在超时期内多次尝试。
实际上对于大型的应用我们都会选择一些大型专业的数据库,System.Data.SQLite和SQLite适合于一些受限的场合,比如手机等。在这里我讲一个真实的经历,在此前我曾经做过一个小型系统,要分析三个Excel文件,其中两个的记录大约在400条左右,而另外一个大约是1万条左右,对于这么一个系统如果使用数据库,即使单机版的Access,导入之后利用数据库的特性进行分析,将是一个相对较为简单的事情,因为我们可以在数据库里使用连接查询,还可以对记录使用数据库函数,但是对方提供的信息是部署的机器上尽管安装了Office,但是只是安装了Word、Excel和Outlook,而没有Access,对方也不希望安装其它的软件,由于我也不能确定没有安装Access的机器上是否能通过OleDB访问.mdb文件,所以没有办法,只有才有内存表的形式,即将Excel中的数据读取到DataTable中,然后对三个DataTable进行分析,尽管做了很多优化,但是效率仍然不是太理想。对于这种情况,如果我当时知道System.Data.SQLite就好办多了,将三个Excel中的数据导入到System.Data.SQLite中,然后利用System.Data.SQLite提供的函数处理起来是相当省事和方便的。对于System.Data.SQLite来说,部署时不需要安装,仅需要一个System.Data.SQLite.dll就够了,这个dll仅866K!而且它不需要像使用Com组件那样需要注册。
在VS2008中操作System.Data.SQLite
为了方便开发者,System.Data.SQLite提供了VS2005和VS2008的支持,甚至还支持.NET 3.5 SP1中的Entity Framework,下面是在VS2008中使用System.Data.SQLite设计器的情况:
首先打开VS2008中的服务器资源管理器,如下图:
 
接着在数据连接上点击鼠标右键,如下图所示:
 
然后选择“添加连接”,如下图所示:
 
这时候选择System.Data.SQLite使用的数据库文件,文件后缀默认是.db3,还可以点击下方的“测试连接”按钮,如果没有问题就会弹出正确的对话框,点击“确定”按钮之后在服务器资源管理器中就会出现如下的情况:
 
这样我们就可以像操作SQL Server中的库一样操作System.Data.SQLite中的表了。
System.Data.SQLite数据库通用类
针对对数据库的操作情况,分为以下几种情况:
创建数据库文件;
返回DataTable;
返回DataReader;
执行增删改,返回受影响的行数;
执行查询,返回第一行第一列(通常用于带有行函数的查询,如SUM/AVG/COUNT等);
返回库中所有的表;
因为在System.Data.SQLite中不存在存储过程,所以所有的操作都是基于文本的SQL语句,为了避免SQL注入,所以使用了参数化的SQL语句,这个数据库通用类如下:
 
  1. using System;  
  2. using System.Data;  
  3. using System.Data.Common;  
  4. using System.Collections.Generic;  
  5. using System.Data.SQLite;  
  6. using System.Reflection;  
  7. using System.Collections;     
  8.  
  9. namespace GetTime  
  10. {  
  11.     public class SQLiteDBHelper  
  12.     {  
  13.         private string connectionString = string.Empty;  
  14.  
  15.         static SQLiteDBHelper()  
  16.         {  
  17.               
  18.         }  
  19.         /// <summary>     
  20.         /// 构造函数     
  21.         /// </summary>     
  22.         /// <param name="dbPath">SQLite数据库文件路径</param>     
  23.         public SQLiteDBHelper(string dbPath)  
  24.         {  
  25.             this.connectionString = "Data Source=" + dbPath;  
  26.         }  
  27.         /// <summary>     
  28.         /// 创建SQLite数据库文件     
  29.         /// </summary>     
  30.         /// <param name="dbPath">要创建的SQLite数据库文件路径</param>     
  31.         public static void CreateDB(string dbPath)  
  32.         {  
  33.             using (SQLiteConnection connection = new SQLiteConnection("Data Source=" + dbPath))  
  34.             {  
  35.                 connection.Open();  
  36.                 using (SQLiteCommand command = new SQLiteCommand(connection))  
  37.                 {  
  38.                     command.CommandText = "CREATE TABLE Demo(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE)";  
  39.                     command.ExecuteNonQuery();  
  40.  
  41.                     command.CommandText = "DROP TABLE Demo";  
  42.                     command.ExecuteNonQuery();  
  43.                 }  
  44.             }  
  45.         }  
  46.         /// <summary>     
  47.         /// 对SQLite数据库执行增删改操作,返回受影响的行数。     
  48.         /// </summary>     
  49.         /// <param name="sql">要执行的增删改的SQL语句</param>     
  50.         /// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>     
  51.         /// <returns></returns>     
  52.         public int ExecuteNonQuery(string sql, IList<SQLiteParameter> parameters)  
  53.         {  
  54.             int affectedRows = 0;  
  55.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))  
  56.             {  
  57.                 connection.Open();  
  58.                 using (DbTransaction transaction = connection.BeginTransaction())  
  59.                 {  
  60.                     using (SQLiteCommand command = new SQLiteCommand(connection))  
  61.                     {  
  62.                         command.CommandText = sql;  
  63.                         if (!(parameters == null || parameters.Count == 0))  
  64.                         {  
  65.                             foreach (SQLiteParameter parameter in parameters)  
  66.                             {  
  67.                                 command.Parameters.Add(parameter);  
  68.                             }  
  69.                         }  
  70.                         affectedRows = command.ExecuteNonQuery();  
  71.                     }  
  72.                     transaction.Commit();  
  73.                 }  
  74.             }  
  75.             return affectedRows;  
  76.         }  
  77.         /// <summary>     
  78.         /// 执行一个查询语句,返回一个关联的SQLiteDataReader实例     
  79.         /// </summary>     
  80.         /// <param name="sql">要执行的查询语句</param>     
  81.         /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>     
  82.         /// <returns></returns>     
  83.         public SQLiteDataReader ExecuteReader(string sql, IList<SQLiteParameter> parameters)  
  84.         {  
  85.             SQLiteConnection connection = new SQLiteConnection(connectionString);  
  86.             SQLiteCommand command = new SQLiteCommand(sql, connection);  
  87.             if (!(parameters == null || parameters.Count == 0))  
  88.             {  
  89.                 foreach (SQLiteParameter parameter in parameters)  
  90.                 {  
  91.                     command.Parameters.Add(parameter);  
  92.                 }  
  93.             }  
  94.             connection.Open();  
  95.             return command.ExecuteReader(CommandBehavior.CloseConnection);  
  96.         }  
  97.         /// <summary>     
  98.         /// 执行一个查询语句,返回一个包含查询结果的DataTable     
  99.         /// </summary>     
  100.         /// <param name="sql">要执行的查询语句</param>     
  101.         /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>     
  102.         /// <returns></returns>     
  103.         public DataTable ExecuteDataTable(string sql, IList<SQLiteParameter> parameters)  
  104.         {  
  105.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))  
  106.             {  
  107.                 using (SQLiteCommand command = new SQLiteCommand(sql, connection))  
  108.                 {  
  109.                     if (!(parameters == null || parameters.Count == 0))  
  110.                     {  
  111.                         foreach (SQLiteParameter parameter in parameters)  
  112.                         {  
  113.                             command.Parameters.Add(parameter);  
  114.                         }  
  115.                     }  
  116.                     SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);  
  117.                     DataTable data = new DataTable();  
  118.                     adapter.Fill(data);  
  119.                     return data;  
  120.                 }  
  121.             }  
  122.  
  123.         }  
  124.         /// <summary>     
  125.         /// 执行一个查询语句,返回查询结果的第一行第一列     
  126.         /// </summary>     
  127.         /// <param name="sql">要执行的查询语句</param>     
  128.         /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准</param>     
  129.         /// <returns></returns>     
  130.         public Object ExecuteScalar(string sql, IList<SQLiteParameter> parameters)  
  131.         {  
  132.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))  
  133.             {  
  134.                 using (SQLiteCommand command = new SQLiteCommand(sql, connection))  
  135.                 {  
  136.                     if (!(parameters == null || parameters.Count == 0))  
  137.                     {  
  138.                         foreach (SQLiteParameter parameter in parameters)  
  139.                         {  
  140.                             command.Parameters.Add(parameter);  
  141.                         }  
  142.                     }  
  143.                     return command.ExecuteScalar();  
  144.                 }  
  145.             }  
  146.         }  
  147.  
  148.         /// <summary>     
  149.         /// 查询数据库中的所有数据类型信息     
  150.         /// </summary>     
  151.         /// <returns></returns>     
  152.         public DataTable GetSchema()  
  153.         {  
  154.             using (SQLiteConnection connection = new SQLiteConnection(connectionString))  
  155.             {  
  156.                 connection.Open();  
  157.                 DataTable data = connection.GetSchema("TABLES");  
  158.                 connection.Close();  
  159.                 //foreach (DataColumn column in data.Columns)     
  160.                 //{     
  161.                 //    Console.WriteLine(column.ColumnName);     
  162.                 //}     
  163.                 return data;  
  164.             }  
  165.         }  
  166.           
  167.     }  
(注:上面的代码在2011-07-15日代码重新编辑了,参数由SQLiteParameter[] parameters变为IList<SQLiteParameter> parameters,使用范围更广)。
System.Data.SQLite数据库通用类的用法
下面演示一下刚刚编写的数据库通用类的用法,代码如下:
using System; 
using System.Collections.Generic; 
using System.Text; 
using System.Data; 
using System.Data.Common; 
using System.Data.SQLite; 
using SQLiteQueryBrowser; 
namespace SQLiteDemo 

         class Program 
        { 
                 static  void Main( string[] args) 
                { 
                         //CreateTable(); 
                         //InsertData(); 
                        ShowData(); 
                        Console.ReadLine(); 
                } 
                 public  static  void CreateTable() 
                { 
                         string dbPath =  "D:\\Demo.db3"
                         //如果不存在改数据库文件,则创建该数据库文件 
                         if (!System.IO.File.Exists(dbPath)) 
                        { 
                                SQLiteDBHelper.CreateDB( "D:\\Demo.db3"); 
                        } 
                        SQLiteDBHelper db =  new SQLiteDBHelper( "D:\\Demo.db3"); 
                         string sql =  "CREATE TABLE Test3(id integer NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,Name char(3),TypeName varchar(50),addDate datetime,UpdateTime Date,Time time,Comments blob)"
                        db.ExecuteNonQuery(sql,  null); 
                } 
                 public  static  void InsertData() 
                { 
                         string sql =  "INSERT INTO Test3(Name,TypeName,addDate,UpdateTime,Time,Comments)values(@Name,@TypeName,@addDate,@UpdateTime,@Time,@Comments)"
                        SQLiteDBHelper db =  new SQLiteDBHelper( "D:\\Demo.db3"); 
                         for ( char c = 'A'; c <= 'Z'; c++) 
                        { 
                                 for ( int i = 0; i < 100; i++) 
                                { 
                                        SQLiteParameter[] parameters =  new SQLiteParameter[]{ 
                                                 new SQLiteParameter( "@Name",c+i.ToString()), 
                                         new SQLiteParameter( "@TypeName",c.ToString()), 
                                         new SQLiteParameter( "@addDate",DateTime.Now), 
                                         new SQLiteParameter( "@UpdateTime",DateTime.Now.Date), 
                                         new SQLiteParameter( "@Time",DateTime.Now.ToShortTimeString()), 
                                         new SQLiteParameter( "@Comments", "Just a Test"+i) 
                                        }; 
                                        db.ExecuteNonQuery(sql, parameters); 
                                } 
                        } 
                } 
                 public  static  void ShowData() 
                { 
                         //查询从50条起的20条记录 
                         string sql =  "select * from test3 order by id desc limit 50 offset 20"
                        SQLiteDBHelper db =  new SQLiteDBHelper( "D:\\Demo.db3"); 
                         using (SQLiteDataReader reader = db.ExecuteReader(sql,  null)) 
                        { 
                                 while (reader.Read()) 
                                { 
                                        Console.WriteLine( "ID:{0},TypeName{1}", reader.GetInt64(0), reader.GetString(1)); 
                                } 
                        } 
                } 
 
        } 
}

在实际情况中,采用通用类大批量插入数据会有些慢,这是因为在System.Data.SQLite中的操作如果没有指定操作,则会被当做一个事物,如果需要一次性写入大量记录,则建议显式创建一个事物,在这个事务中完成所有的操作比较好,这样的话比每次操作创建一个事物的效率要提升很多。
最终利用VS2008提供的功能,可以看到里面的数据如下:
 
需要说明的是在System.Data.SQLite中数据类型的规定不适很严格,从创建Test3表的SQL语句来看,表中addDate、UpdateTime、Time分别是DateTime、Date、Time类型字段,但实际上我们插入的时候没有按照这个规定,最终显示的结果也是尽量遵循数据库字段的定义。
总结
System.Data.SQLite确实是一个非常小巧精悍的数据库,作为对SQLite的封装(SQLite可以在Android等类型的手机上利用Java访问),它依然是体较小,同比性能高、内存消耗小、无需安装仅需一个dll就可以运行的优点(如果在Mobile手机上则需要两个文件),唯一的一个缺点是没有比较的GUI(图形用户界面),不过正因为如此它才得以体积小。
在实际开发中没有图形用户界面可能有些不便,我们可以使用VS来查看和操作数据,我自己也做了一个小东东,便于管理和维护数据,界面如下:
 
如果你要开发数据量在10万条以下的应用,我建议你尝试使用一下System.Data.SQLite,它或许是一个不错的选择。






















本文转自周金桥51CTO博客,原文链接: http://blog.51cto.com/zhoufoxcn/292670 ,如需转载请自行联系原作者








相关文章
|
3月前
|
数据库 数据库管理
System.ArgumentException:“The specified invariant name ‘System.Data.SQLite‘ wasn‘t found in the list
System.ArgumentException:“The specified invariant name ‘System.Data.SQLite‘ wasn‘t found in the list
15 0
|
5月前
|
数据库管理 Windows
【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
【CodeSmith】The System.Data.SQLite library is not installed on this computer,不能使用SQLite解决办法
21 0
|
安全 NoSQL MongoDB
【安装MongoDB报错】mkdir: /data/db: Read-only file system
在安装MongoDB时,需要创建一个/data/db文件夹用来作为默认数据库目录。 但是因为Mac电脑默认是开启安全模式的,不能在根目录下面随便创建删除文件夹。所以我们创建的时候,会报这个错误mkdir: /data/db: Read-only file system,
561 0
|
存储 SQL 缓存
我为什么用 SQLite 和 FMDB 而不用 Core Data
凭良心讲,我不能告诉你不去使用Core Data。它不错,而且也在变好,并且它被很多其他Cocoa开发者所理解,当有新人加入你的组或者需要别人接手你的项目的时候,这点很重要。 更重要的是,不值得花时间和精力去写自己的系统去代替它。真的,使用Core Data吧。
149 0
|
存储 C# 数据库
System.Data.SQLite&nbsp;中GUID的处理
原文:System.Data.SQLite 中GUID的处理 项目中正好用到System.Data.SQLite,在手持上使用这个数据库,因为要做数据同步,所以表中的主键都是Guid的数据类型。
1037 0
|
SQL 数据库 数据库管理