SQLite Helper (C#) z
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
- GetTableStatus
- GetColumnStatus
- CreateTable
- BeginTransaction, Commit, Rollback
- Select
- Execute
- ExecuteScalar
- ExecuteScalarStr
- ExecuteScalarInt
- ExecuteScalarDateTime
- ExecuteScalarDecimal
- ExecuteScalarBlob
- Escape
- Insert
- Update
- RenameTable
- CopyAllData
- DropTable
Getting Start
Add this using statement at the top of your class:
Collapse | Copy Codeusing System.Data.SQLite;
SQLiteConnection and SQLiteCommand have to be initialized before using SQLiteHelper:
Example:
Collapse | Copy Codeusing (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 CodeDataTable 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 CodeSQLiteTable 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 Codesh.BeginTransaction(); try
{
// execute some queries sh.Commit();
}
catch
{
sh.Rollback();
}
5. Select
Return the query result in DataTable format.
Collapse | Copy CodeDataTable dt = sh.Select("select * from person order by id;");
6. Execute
Execute single SQL query.
Collapse | Copy Codesh.Execute("insert into person(name)values('hello');");
7. ExecuteScalar
Return the result from first row first column in object format.
Collapse | Copy Codeobject ob = sh.ExecuteScalar("select max(id) from person;");
8. ExecuteScalarStr
Return the result from first row first column in string format.
Collapse | Copy Codestring s = sh.ExecuteScalarStr("select max(id) from person;");
9. ExecuteScalarInt
Return the result from first row first column in Int format.
Collapse | Copy Codeint i = sh.ExecuteScalarInt("select max(id) from person;");
10. ExecuteScalarDateTime
Return the result from first row first column in DateTime format.
Collapse | Copy CodeDateTime 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 Codedecimal 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 Codebyte[] 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 Codestring value = sh.Escape(input);
14. Insert
Insert data.
Sample 1: Insert single row.
Collapse | Copy Codevar 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 Codevar 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 Codevar 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 Codevar 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 Codesh.RenameTable("person", "person_backup");
17. CopyAllData
Copy all data from one table to another.
Collapse | Copy Codesh.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 Codesh.DropTable("person");
That's it, guys/girls. Comments are welcome.
Happy coding 
Also available at: https://sh.codeplex.com/[^]
SQLite Helper (C#) z的更多相关文章
- sqlite helper
//-------------------------------------------------------------------------- // // Copyright (c) BUS ...
- SQLite Helper (C#) zt
http://www.codeproject.com/Articles/746191/SQLite-Helper-Csharp This small class (SQLiteHelper.cs) i ...
- C#操作SQLite数据库
SQLite介绍 SQLite is a software library that implements a self-contained, serverless, zero-configurati ...
- Android中多表的SQLite数据库(译)
原文: Android SQLite Database with Multiple Tables 在上一篇教程Android SQLite Database Tutorial中,解释了如何在你的And ...
- C# SQLite 创建数据库的方法增删查改语法和命令
SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...
- 安卓APP与智能硬件相结合的简易方案
第1章 概 述 (作者小波QQ463431476) (来源http://blog.chinaaet.com/zhaocundang/p/5100017645博客) (来源 http://www. ...
- HRPlugin For Xcode发布(附源码地址)
今天给大家介绍的这个插件,是我在IOS平台上开发以来,一些想法的集合体.因为本人时常感觉在开发过程中无论从GOOGLE资料查找和SQL数据库查询,正则表达式测试,SVN等,这些经常要做的操作中,耽误了 ...
- 学习笔记:调用js文件冲突问题解决方案
之前自己动手做了一个小网站,在实现过程中遇到了一个关于js文件调用冲突的问题. 具体问题描述如下:在index.html文件中引用了两个js文件,单独添加banner.js或者focus_pic.js ...
- 使用Rxjava缓存请求
最近,我尝试使用RxJava开发了一款闲时备份app.我必须承认,一旦你get到了正确的方式,RxJava几乎感觉就像作弊.一切看起来更简洁,多个请求能够被组合,且非常容易控制.通过在UI线程观察和在 ...
随机推荐
- jekyll bootstrap更改主题theme
使用主题 介绍: 由于JB版本号0.2.X的主题,如今全然是模块化的.他们跟踪和单独版本号的主题包. 这让每一个人都能够自由公布和共享主题. Jekyll-Bootstrap v 0.2.x仅仅附带t ...
- JavaScript实现竖直文本滚动
一.HTML代码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://ww ...
- Android-ViewPagerIndicator
https://github.com/JakeWharton/Android-ViewPagerIndicator
- java实现window phone推送通知
package com.windowphone.text; import java.io.IOException;import java.io.OutputStream;import java.net ...
- Why String is immutable in Java ?--reference
String is an immutable class in Java. An immutable class is simply a class whose instances cannot be ...
- 【S】【S】【S】一大波前端干货整合(一)
前端交流站点 大前端 http://www.daqianduan.com/ V2EX http://www.v2ex.com/ W3cplus http://www. ...
- RazorEngine在非MVC下的使用,以及使用自定义模板
---恢复内容开始--- RazorEngine模板引擎大大的帮助了我们简化字符串的拼接与方法的调用,开源之后,现在在简单的web程序,winform程序,甚至控制台程序都可以利用它来完成. 但如何在 ...
- maven是什么?(转自oracle官网)
Maven 是一个项目管理和构建自动化工具.但是对于我们程序员来说,我们最关心的是它的项目构建功能.所以这里我们介绍的就是怎样用 maven 来满足我们项目的日常需要.Maven 使用惯例优于配置的原 ...
- .net的 async 和 await
async 和 await 出现在C# 5.0之后,关系是两兄弟,Task是父辈,Thread是爷爷辈,这就是.net 多线程处理的东西,具体包括 创建线程,线程结果返回,线程中止,线程中的异常处理 ...
- Java中的static关键字解析(转自海子)__为什么main方法必须是static的,因为程序在执行main方法的时候没有创建任何对象,因此只有通过类名来访问。
Java中的static关键字解析 static关键字是很多朋友在编写代码和阅读代码时碰到的比较难以理解的一个关键字,也是各大公司的面试官喜欢在面试时问到的知识点之一.下面就先讲述一下static关键 ...