http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp

Introduction

I have written a small class, SQLiteHelper which aims to simplify the usage of SQLite in C#.

Prerequisite

This small class is built on top of System.Data.SQLite.DLL. A reference of this DLL must be added into your projects.

Download: https://system.data.sqlite.org

List of Simplified Functions

  1. GetTableStatus
  2. GetColumnStatus
  3. CreateTable
  4. BeginTransaction, Commit, Rollback
  5. Select
  6. Execute
  7. ExecuteScalar
  8. ExecuteScalarStr
  9. ExecuteScalarInt
  10. ExecuteScalarDateTime
  11. ExecuteScalarDecimal
  12. ExecuteScalarBlob
  13. Escape
  14. Insert
  15. Update
  16. RenameTable
  17. CopyAllData
  18. DropTable

Getting Start

Add this using statement at the top of your class:

Collapse | Copy Code
using System.Data.SQLite;

SQLiteConnection and SQLiteCommand have to be initialized before using SQLiteHelper:

Example:

Collapse | Copy Code
using (SQLiteConnection conn = new SQLiteConnection("data source=C:\\data"))
{
using (SQLiteCommand cmd = new SQLiteCommand())
{
cmd.Connection = conn;
conn.Open(); SQLiteHelper sh = new SQLiteHelper(cmd); // do something... conn.Close();
}
}

1. GetTableStatus

Get all information of tables in the database.

Collapse | Copy Code
DataTable dt = sh.GetTableStatus();

2. GetColumnStatus

Get all information of columns in specific table.

Collapse | Copy Code
// Get column's information from table "person"
DataTable dt = sh.GetColumnStatus("person");

3. CreateTable

Create table.

Example table structure: Person

Column Name Data Type Primary Key Not Null Default Value
id int true    
name text      
membershipid int      
level decimal     5.5
Collapse | Copy Code
SQLiteTable tb = new SQLiteTable("person");

tb.Columns.Add(new SQLiteColumn("id", true));
tb.Columns.Add(new SQLiteColumn("name"));
tb.Columns.Add(new SQLiteColumn("membershipid", ColType.Integer));
tb.Columns.Add(new SQLiteColumn("level", ColType.Decimal, false, false, "5.5")); sh.CreateTable(tb);

4. BeginTransaction, Commit, Rollback

Applying transactions.

Collapse | Copy Code
sh.BeginTransaction();

try
{
// execute some queries sh.Commit();
}
catch
{
sh.Rollback();
}

5. Select

Return the query result in DataTable format.

Collapse | Copy Code
DataTable dt = sh.Select("select * from person order by id;");

6. Execute

Execute single SQL query.

Collapse | Copy Code
sh.Execute("insert into person(name)values('hello');");

7. ExecuteScalar

Return the result from first row first column in object format.

Collapse | Copy Code
object ob = sh.ExecuteScalar("select max(id) from person;");

8. ExecuteScalarStr

Return the result from first row first column in string format.

Collapse | Copy Code
string s = sh.ExecuteScalarStr("select max(id) from person;");

9. ExecuteScalarInt

Return the result from first row first column in Int format.

Collapse | Copy Code
int i = sh.ExecuteScalarInt("select max(id) from person;");

10. ExecuteScalarDateTime

Return the result from first row first column in DateTime format.

Collapse | Copy Code
DateTime date = sh.ExecuteScalarDateTime("select dateregister from person where id = 1;");

11. ExecuteScalarDecimal

Return the result from first row first column in decimal format.

Collapse | Copy Code
decimal d = sh.ExecuteScalarDecimal("select level from person where id = 1;");

12. ExecuteScalarBlob

Return the result from first row first column in byte[] format.

Collapse | Copy Code
byte[] ba = sh.ExecuteScalarBlob("select photo from person where id = 1;");

13. Escape

Escape string sequence for text value to avoid SQL injection or invalid SQL syntax to be constructed.

Collapse | Copy Code
string value = sh.Escape(input);

14. Insert

Insert data.

Sample 1: Insert single row.

Collapse | Copy Code
var dic = new Dictionary<string, object>();
dic["name"] = "John";
dic["membershipid"] = 1;
dic["level"] = 6.8; sh.Insert("person", dic);

Sample 2: Insert multiple rows.

Collapse | Copy Code
var lst = new List<Dictionary<string,>();

var dic1 = new Dictionary<string,>();
dic1["name"] = "John";
dic1["membershipid"] = 1;
dic1["level"] = 6.8;
lst.Add(dic1); var dic2 = new Dictionary<string,>();
dic2["name"] = "Catherine";
dic2["membershipid"] = 2;
dic2["level"] = 9.7;
lst.Add(dic2); var dic3 = new Dictionary<string,>();
dic3["name"] = "Thomas";
dic3["membershipid"] = 3;
dic3["level"] = 8.6;
lst.Add(dic3); sh.Insert("person", lst);

16. Update

Update row.

Sample 1: Update with single condition (where id = 1)

Collapse | Copy Code
var dicData = new Dictionary<string, object>();
dicData["name"] = "no name";
dicData["membershipid"] = 0;
dicData["level"] = 5.5; sh.Update("person", dicData, "id", 1);

Sample 2: Update with multiple condition (where membership = 1 and level = 5.5)

Collapse | Copy Code
var dicData = new Dictionary<string, object>();
dicData["name"] = "no name";
dicData["status"] = 0;
dicData["money"] = 100;
dicData["dateregister"] = DateTime.MinValue; var dicCondition = new Dictionary<string,>();
dicCondition["membershipid"] = 1;
dicData["level"] = 5.5; sh.Update("person", dicData, dicCondition);

16. RenameTable

Rename a table.

Collapse | Copy Code
sh.RenameTable("person", "person_backup");

17. CopyAllData

Copy all data from one table to another.

Collapse | Copy Code
sh.CopyAllData("person", "person_new");

Before copying, SQLiteHelper will scan the two tables for match columns. Only columns that exist in both tables will be copied.

18. DropTable

Drop table, delete a table

Collapse | Copy Code
sh.DropTable("person");

That's it, guys/girls. Comments are welcome.

Happy coding

Also available at: https://sh.codeplex.com/[^]

SQLite Helper (C#) z的更多相关文章

  1. sqlite helper

    //-------------------------------------------------------------------------- // // Copyright (c) BUS ...

  2. SQLite Helper (C#) zt

    http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp This small class (SQLiteHelper.cs) i ...

  3. C#操作SQLite数据库

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

  4. Android中多表的SQLite数据库(译)

    原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...

  5. C# SQLite 创建数据库的方法增删查改语法和命令

    SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...

  6. 安卓APP与智能硬件相结合的简易方案

    第1章 概  述 (作者小波QQ463431476) (来源http://blog.chinaaet.com/zhaocundang/p/5100017645博客) (来源   http://www. ...

  7. HRPlugin For Xcode发布(附源码地址)

    今天给大家介绍的这个插件,是我在IOS平台上开发以来,一些想法的集合体.因为本人时常感觉在开发过程中无论从GOOGLE资料查找和SQL数据库查询,正则表达式测试,SVN等,这些经常要做的操作中,耽误了 ...

  8. 学习笔记:调用js文件冲突问题解决方案

    之前自己动手做了一个小网站,在实现过程中遇到了一个关于js文件调用冲突的问题. 具体问题描述如下:在index.html文件中引用了两个js文件,单独添加banner.js或者focus_pic.js ...

  9. 使用Rxjava缓存请求

    最近,我尝试使用RxJava开发了一款闲时备份app.我必须承认,一旦你get到了正确的方式,RxJava几乎感觉就像作弊.一切看起来更简洁,多个请求能够被组合,且非常容易控制.通过在UI线程观察和在 ...

随机推荐

  1. java基础之synchronized使用方法

    首先.參考文章:http://www.cnblogs.com/devinzhang/archive/2011/12/14/2287675.html PS:參考文章非常长,但内容非常丰富,若是刚開始学习 ...

  2. FindStringExact

          Code:: CComboBox::FindStringExact int FindStringExact( int nIndexStart, LPCTSTR lpszFind ) con ...

  3. [React] React Fundamentals: First Component

    <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...

  4. cocos2dx js文件加密为jsc文件

    发布产品,脚本代码是必须要加密的 偶尔会出现编译后的jsc无法运行,或者某些jsb自定义的函数找不到, 最好将require("jsb.js")的全部内容整合到一个文件,然后编译j ...

  5. string的操作

    除了顺序容器共有的操作之外,string类型还提供了一些额外的操作.这些操作中的大部分要么是提供string类和C风格字符数组之间的相互转换,要么是增加了允许我们用下标代替迭代器的版本. 构造stri ...

  6. linux mysql 卸载后重装

    $sudo apt-get remove mysql-common清理残留数据:$sudo dpkg -l |grep ^rc|awk '{print $2}' |sudo xargs dpkg -P ...

  7. Spring MVC 3.0.5+Spring 3.0.5+MyBatis3.0.4全注解实例详解(五)

    这是本系列的最后一篇,主要讲一下FreeMarker模板引擎的基本概念与常用指令的使用方式.     一.FreemMarker基本概念     FreemMarker是一个用Java语言编写的模板引 ...

  8. C#读取Exeal文件

    今天写一个读取Exeal的时候遇到一个问题就是引用了Mircosotf.Office.Interop.Exeal类库的时候没有办法读取到 纠结了好久百度了一下发现别人是这样写的using Exeal= ...

  9. C#读取Excel表中的数据时,为何有些行的字段内容读取不到

    转载:http://bbs.csdn.net/topics/360220285 1.当某列数据中含有混合类型时,在.NET中使用Microsoft.Jet.OLEDB.4.0来读取Excel文件造成数 ...

  10. 对进度条progressbar的调整

    进度条的理解,感觉这个进度条不是那么简单,系统给我们定制了几个普通的,但是如果还需要有更加好的效果,需要自己去调试. <ProgressBar android:layout_width=&quo ...