热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SQLiteHelper(C#)z

http:www.codeproject.comArticles746191SQLite-Helper-Csharp IntroductionIhavewrittenasmallc

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:

《SQLite Helper (C#) z》
Collapse
|
Copy Code

using System.Data.SQLite;

 

SQLiteConnection and SQLiteCommand have to be initialized before using SQLiteHelper:

Example:

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

DataTable dt = sh.GetTableStatus();

2. GetColumnStatus

Get all information of columns in specific table.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

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

3. CreateTable

Create table.

Example table structure: Person

Column NameData TypePrimary KeyNot NullDefault Value
idinttrue  
nametext   
membershipidint   
leveldecimal  5.5

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

sh.BeginTransaction();
try
{
// execute some queries

sh.Commit();
}
catch
{
sh.Rollback();
}

5. Select

Return the query result in DataTable format.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

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

6. Execute

Execute single SQL query.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

sh.Execute("insert into person(name)values('hello');");

7. ExecuteScalar

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

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

string value = sh.Escape(input);

14. Insert

Insert data.

Sample 1: Insert single row.

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

var lst = new Liststring,>();
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)

《SQLite Helper (C#) z》
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)

《SQLite Helper (C#) z》
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.

《SQLite Helper (C#) z》
Collapse
|
Copy Code

sh.RenameTable("person", "person_backup");

17. CopyAllData

Copy all data from one table to another.

《SQLite Helper (C#) z》
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

《SQLite Helper (C#) z》
Collapse
|
Copy Code

sh.DropTable("person");

 

That&#8217;s it, guys/girls. Comments are welcome.

Happy coding 《SQLite Helper (C#) z》

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


推荐阅读
  • MyBatis多表查询与动态SQL使用
    本文介绍了MyBatis多表查询与动态SQL的使用方法,包括一对一查询和一对多查询。同时还介绍了动态SQL的使用,包括if标签、trim标签、where标签、set标签和foreach标签的用法。文章还提供了相关的配置信息和示例代码。 ... [详细]
  • 在Oracle11g以前版本中的的DataGuard物理备用数据库,可以以只读的方式打开数据库,但此时MediaRecovery利用日志进行数据同步的过 ... [详细]
  • 本文介绍了数据库的存储结构及其重要性,强调了关系数据库范例中将逻辑存储与物理存储分开的必要性。通过逻辑结构和物理结构的分离,可以实现对物理存储的重新组织和数据库的迁移,而应用程序不会察觉到任何更改。文章还展示了Oracle数据库的逻辑结构和物理结构,并介绍了表空间的概念和作用。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 高质量SQL书写的30条建议
    本文提供了30条关于优化SQL的建议,包括避免使用select *,使用具体字段,以及使用limit 1等。这些建议是基于实际开发经验总结出来的,旨在帮助读者优化SQL查询。 ... [详细]
  • 本文介绍了在CentOS上安装Python2.7.2的详细步骤,包括下载、解压、编译和安装等操作。同时提供了一些注意事项,以及测试安装是否成功的方法。 ... [详细]
  • 本文介绍了一种轻巧方便的工具——集算器,通过使用集算器可以将文本日志变成结构化数据,然后可以使用SQL式查询。集算器利用集算语言的优点,将日志内容结构化为数据表结构,SPL支持直接对结构化的文件进行SQL查询,不再需要安装配置第三方数据库软件。本文还详细介绍了具体的实施过程。 ... [详细]
  • 微软头条实习生分享深度学习自学指南
    本文介绍了一位微软头条实习生自学深度学习的经验分享,包括学习资源推荐、重要基础知识的学习要点等。作者强调了学好Python和数学基础的重要性,并提供了一些建议。 ... [详细]
  • 使用nodejs爬取b站番剧数据,计算最佳追番推荐
    本文介绍了如何使用nodejs爬取b站番剧数据,并通过计算得出最佳追番推荐。通过调用相关接口获取番剧数据和评分数据,以及使用相应的算法进行计算。该方法可以帮助用户找到适合自己的番剧进行观看。 ... [详细]
  • 本文详细介绍了MySQL表分区的创建、增加和删除方法,包括查看分区数据量和全库数据量的方法。欢迎大家阅读并给予点评。 ... [详细]
  • 闭包一直是Java社区中争论不断的话题,很多语言都支持闭包这个语言特性,闭包定义了一个依赖于外部环境的自由变量的函数,这个函数能够访问外部环境的变量。本文以JavaScript的一个闭包为例,介绍了闭包的定义和特性。 ... [详细]
  • Oracle seg,V$TEMPSEG_USAGE与Oracle排序的关系及使用方法
    本文介绍了Oracle seg,V$TEMPSEG_USAGE与Oracle排序之间的关系,V$TEMPSEG_USAGE是V_$SORT_USAGE的同义词,通过查询dba_objects和dba_synonyms视图可以了解到它们的详细信息。同时,还探讨了V$TEMPSEG_USAGE的使用方法。 ... [详细]
author-avatar
到几百元_309
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有