SQLite特点
(1)轻量级,跨平台的关系型数据库,所以支持视图,事务,触发器等。
(2)零配置-无需安装和管理配置,存储在单一磁盘文件中的完整的数据库
(3)数据库文件可共享,支持多种开发语言。
 
SQLite应用场景
适用于并发量低,访问需求高于写入操作的一些引用
例如,最近的项目里,在客户的文件服务器下,需要要建立文件路径的索引,并能够文件打包下载,考虑到不想装单独的数据库,于是乎便用了SQLite
 
SQLite使用
封装SQliteHelper类。
  1. public static class SqliteHelper
  2. {
  3. private static string connectionString = ConfigurationManager.ConnectionStrings["SqliteConnectionstring"].ConnectionString;
  4.  
  5. /// <summary>
  6. /// 创建一个数据库文件。如果存在同名数据库文件,则会覆盖。
  7. /// </summary>
  8. /// <param name="dbName">数据库文件名。为null或空串时不创建。</param>
  9. /// <param name="password">(可选)数据库密码,默认为空。</param>
  10. /// <exception cref="Exception"></exception>
  11. public static void CreateDB(string dbName)
  12. {
  13. SQLiteConnection conn = null;
  14. string dbPath = "Data Source ="+ connectionString+"/"+dbName + ".db";
  15. conn = new SQLiteConnection(dbPath);//创建数据库实例,指定文件位置
  16. conn.Open();//打开数据库,若文件不存在会自动创建
  17. string sql = "CREATE TABLE IF NOT EXISTS T_BuildChildPath(Id integer IDENTITY(1,1), ProductsCode nvarchar(100), CodePath nvarchar(100), CreateTime datetime);";//建表语句
  18. SQLiteCommand cmdCreateTable = new SQLiteCommand(sql, conn);
  19. cmdCreateTable.ExecuteNonQuery();//如果表不存在,创建数据表
  20. conn.Close();
  21. }
  22. /// <summary>
  23. /// 对SQLite数据库执行增删改操作,返回受影响的行数。
  24. /// </summary>
  25. /// <param name="sql">要执行的增删改的SQL语句。</param>
  26. /// <param name="parameters">执行增删改语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
  27. /// <returns></returns>
  28. /// <exception cref="Exception"></exception>
  29. public static int ExecuteNonQuery(string sql, params SQLiteParameter[] parameters)
  30. {
  31. int affectedRows = ;
  32. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  33. {
  34. using (SQLiteCommand command = new SQLiteCommand(connection))
  35. {
  36. try
  37. {
  38. connection.Open();
  39. command.CommandText = sql;
  40.  
  41. if (parameters.Length != )
  42. {
  43. command.Parameters.AddRange(parameters);
  44. }
  45. affectedRows = command.ExecuteNonQuery();
  46. }
  47. catch (Exception ex) { throw; }
  48. }
  49. }
  50. return affectedRows;
  51. }
  52. public static int ExecuteNonQuery(string sql )
  53. {
  54. int affectedRows = ;
  55. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  56. {
  57. using (SQLiteCommand command = new SQLiteCommand(connection))
  58. {
  59. try
  60. {
  61. connection.Open();
  62. command.CommandText = sql;
  63.  
  64. affectedRows = command.ExecuteNonQuery();
  65. }
  66. catch (Exception ex) { throw; }
  67. }
  68. }
  69. return affectedRows;
  70. }
  71. /// <summary>
  72. /// 批量处理数据操作语句。
  73. /// </summary>
  74. /// <param name="list">SQL语句集合。</param>
  75. /// <exception cref="Exception"></exception>
  76. public static void ExecuteNonQueryBatch(List<KeyValuePair<string, SQLiteParameter[]>> list)
  77. {
  78. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  79. {
  80. try { conn.Open(); }
  81. catch { throw; }
  82. using (SQLiteTransaction tran = conn.BeginTransaction())
  83. {
  84. using (SQLiteCommand cmd = new SQLiteCommand(conn))
  85. {
  86. try
  87. {
  88. foreach (var item in list)
  89. {
  90. cmd.CommandText = item.Key;
  91. if (item.Value != null)
  92. {
  93. cmd.Parameters.AddRange(item.Value);
  94. }
  95. cmd.ExecuteNonQuery();
  96. }
  97. tran.Commit();
  98. }
  99. catch (Exception) { tran.Rollback(); throw; }
  100. }
  101. }
  102. }
  103. }
  104. /// <summary>
  105. /// 执行查询语句,并返回第一个结果。
  106. /// </summary>
  107. /// <param name="sql">查询语句。</param>
  108. /// <returns>查询结果。</returns>
  109. /// <exception cref="Exception"></exception>
  110. public static object ExecuteScalar(string sql, params SQLiteParameter[] parameters)
  111. {
  112. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  113. {
  114. using (SQLiteCommand cmd = new SQLiteCommand(conn))
  115. {
  116. try
  117. {
  118. conn.Open();
  119. cmd.CommandText = sql;
  120. if (parameters.Length != )
  121. {
  122. cmd.Parameters.AddRange(parameters);
  123. }
  124. return cmd.ExecuteScalar();
  125. }
  126. catch (Exception) { throw; }
  127. }
  128. }
  129. }
  130. public static bool ExecuteScalar(string sql )
  131. {
  132. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  133. {
  134. using (SQLiteCommand cmd = new SQLiteCommand(conn))
  135. {
  136. try
  137. {
  138. conn.Open();
  139. cmd.CommandText = sql;
  140.  
  141. return (Convert.ToInt32(cmd.ExecuteScalar()?.ToString()??"")>)?true:false;
  142. }
  143. catch (Exception) { throw; }
  144. }
  145. }
  146. }
  147. public static string ExecuteScalarStr(string sql)
  148. {
  149. using (SQLiteConnection conn = new SQLiteConnection(connectionString))
  150. {
  151. using (SQLiteCommand cmd = new SQLiteCommand(conn))
  152. {
  153. try
  154. {
  155. conn.Open();
  156. cmd.CommandText = sql;
  157. return cmd.ExecuteScalar()?.ToString();
  158. }
  159. catch (Exception) { throw; }
  160. }
  161. }
  162. }
  163. /// <summary>
  164. /// 执行一个查询语句,返回一个包含查询结果的DataTable。
  165. /// </summary>
  166. /// <param name="sql">要执行的查询语句。</param>
  167. /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
  168. /// <returns></returns>
  169. /// <exception cref="Exception"></exception>
  170. public static DataTable ExecuteQuery(string sql, params SQLiteParameter[] parameters)
  171. {
  172. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  173. {
  174. using (SQLiteCommand command = new SQLiteCommand(sql, connection))
  175. {
  176. if (parameters.Length != )
  177. {
  178. command.Parameters.AddRange(parameters);
  179. }
  180. SQLiteDataAdapter adapter = new SQLiteDataAdapter(command);
  181. DataTable data = new DataTable();
  182. try { adapter.Fill(data); }
  183. catch (Exception) { throw; }
  184. return data;
  185. }
  186. }
  187. }
  188. /// <summary>
  189. /// 执行一个查询语句,返回一个关联的SQLiteDataReader实例。
  190. /// </summary>
  191. /// <param name="sql">要执行的查询语句。</param>
  192. /// <param name="parameters">执行SQL查询语句所需要的参数,参数必须以它们在SQL语句中的顺序为准。</param>
  193. /// <returns></returns>
  194. /// <exception cref="Exception"></exception>
  195. public static SQLiteDataReader ExecuteReader(string sql, params SQLiteParameter[] parameters)
  196. {
  197. SQLiteConnection connection = new SQLiteConnection(connectionString);
  198. SQLiteCommand command = new SQLiteCommand(sql, connection);
  199. try
  200. {
  201. if (parameters.Length != )
  202. {
  203. command.Parameters.AddRange(parameters);
  204. }
  205. connection.Open();
  206. return command.ExecuteReader(CommandBehavior.CloseConnection);
  207. }
  208. catch (Exception) { throw; }
  209. }
  210. /// <summary>
  211. /// 查询数据库中的所有数据类型信息。
  212. /// </summary>
  213. /// <returns></returns>
  214. /// <exception cref="Exception"></exception>
  215. public static DataTable GetSchema()
  216. {
  217. using (SQLiteConnection connection = new SQLiteConnection(connectionString))
  218. {
  219. try
  220. {
  221. connection.Open();
  222. return connection.GetSchema("TABLES");
  223. }
  224. catch (Exception) { throw; }
  225. }
  226. }
  227. }

这里的SqliteConnectionstring连接字符串和SQL类似,Data Source改成对应的物理路径即可。

  1. <add name="SqliteConnectionstring" connectionString="Data Source=D:/ProductsIndex.db" providerName="System.Data.SqlClient" />

页面的调用:

  1. string sql = string.Format(@" select * from T_BuildChildPath where ProductsCode like '%" + PartCode + "%'");
  2. DataView dv = SqliteHelper.ExecuteQuery(sql).DefaultView;

SQLite介绍和使用的更多相关文章

  1. (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解

    原文:http://blog.csdn.net/lidew521/article/details/8655229 1.SQLite介绍:最大特点是,无数据类型;除了可以使用文件或SharedPrefe ...

  2. SQLite介绍、学习笔记、性能测试

    SQLite介绍.学习笔记.性能测试 哪些人,哪些公司或软件在用SQLite: Nokia's Symbian,Mozilla,Abobe,Google,阿里旺旺,飞信,Chrome,FireFox可 ...

  3. 【数据库】 SQLite 介绍

    [数据库] SQLite 介绍 一. 特点 : 小而精悍 1. 轻量级 : 占用资源低 2. 嵌入式 : 无需安装,直接引用就可用 3. 支持 SQL 语法, 大部分兼容 Sql Server 语法, ...

  4. 学习笔记——SQLite介绍

    简介:Google为android的较大数据的处理提供了SQLlite, 他在数据存储.管理.维护.等各方面都相当出色功能也非常强大. 1.创建数据库 Android 为了让我们能够更加方便地管理数据 ...

  5. Android中SQLite介绍

    现在的主流移动设备像Android.iPhone等都使用SQLite作为复杂数据的存储引擎,在我们为移动设备开发应用程序时,也许就要使用到SQLite来存储我们大量的数据,所以我们就需要掌握移动设备上 ...

  6. SQLite介绍及使用

    SQLite,是一款轻型的数据库,是遵守ACID的关联式数据库管理系统,它的设计目标是嵌入式的,而且目前已经在很多嵌入式产品中使用了它,它占用资源非常的低,在嵌入式设备中,可能只需要几百K的内存就够了 ...

  7. SQLite介绍

    优点:轻量级.绿色组件.单一文件.跨平台.查询效率极高.使用事务插入速度极快.支持limit分页. 适合查询速度要求较高,内存占用较少的场合,尤其是嵌入式操作系统,如各种手机操作系统,低并发web(9 ...

  8. C#操作SQLite数据库

    SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configurati ...

  9. 数据库SQLite

    一.数据库 在项目开发中,通常都需要对数据进行离线缓存的处理,如新闻数据的离线缓存等.离线缓存一般都是把数据保存到项目的沙盒中.有以下几种方式: 归档:NSKeyedArchiver 偏好设置:NSU ...

随机推荐

  1. ServerManager.exe 0xc0000135 应用程序错误

    问题: 将 Windows Server 2016 .NET Framework移除. IIS卸载后, Server Manager.exe.事件查看器等都无法正常开启. 解决方案: 在运行中,输入C ...

  2. linux下安装 ping 命令

    使用docker仓库下载的ubuntu 14.04 镜像.里面精简的连 ping 命令都没有.google 百度都搜索不到ping 命令在哪个包里. 努力找了半天,在一篇文章的字里行间发现了 ping ...

  3. KAFKA && zookeeper 集群安装

    服务器:#vim /etc/hosts10.16.166.90 sh-xxx-xxx-xxx-online-0110.16.168.220 sh-xx-xxx-xxx-online-0210.16.1 ...

  4. 【Python】[技术博客] 如何对使用PYQT编写的GUI文件进行单元测试

    如何对使用PYQT编写的GUI文件进行单元测试 想要对PYQT编写的GUI文件进行单元测试,我们主要用到QTest QTest里面包含了一些对窗体的各种控件进行模拟操作的函数,通过QTest对窗体进行 ...

  5. sql server查看表大小

    查看SqlServer 数据库中各个表多少行 : SELECT A.NAME ,B.ROWS FROM sysobjects A JOIN sysindexes B ON A.id = B.id WH ...

  6. 在线生成安卓APP图标

    移动应用图标/启动图生成工具,一键生成所有尺寸的应用图标/启动图 在线生成安卓APP图标生成 图标在 线 在线图标 安卓图标 生成图标 https://icon.wuruihong.com/ 在线pn ...

  7. 解决ffmpeg拉流转发频繁丢包问题max delay reached. need to consume packet

    软件: 1.流媒体服务器EasyDarwin-windows-8.1.0-1901141151 2.ffmpeg-20181001-dcbd89e-win64-static 3.直播源:rtsp:// ...

  8. 【Python】解析Python中的文件操作

    目录结构: contents structure [-] 简介 Python中的文件类型 内置函数的文件操作 open()函数 Mode 创建文本文件 读取文本文件 循环文件对象 关闭文件 With语 ...

  9. Java12新特性 -- 其他新增,移除,废弃项

    支持unicode 11 JDK 12版本包括对Unicode 11.0.0的支持.在发布支持Unicode 10.0.0的JDK 11之后,Unicode 11.0.0引 入了以下JDK 12中包含 ...

  10. WMS 162服务器空间满,清日志

    /usr/local/tomcat-ibus/logs/usr/local/tomcat-ibus/bin/logs/usr/local/tomcat-api/bin/logs/usr/local/t ...