SQLite使用

摘要:
//////包含数据库的文件publicSQLiteDatabase{dbConnection=String.Format;}//////指定高级连接选项的单个Param构造函数///////包含所有必需选项及其值的字典publicSQLiteDatabase{Stringstr=“”;foreach{str+=String.Format;}str=str.Trim()。基底;dbConnection=str;}//////允许程序员对数据库运行查询///////TheSQLtorun///包含结果集的ADataTable。publicDataTableGetDataTable{DataTabledt=newDataTable();try{SQLiteConnectioncnn=newSQLiteConnection;cnn.Open();SQLiteCommandmycommand=newSQLiteCommand;mycommand.CommandText=sql;SQLiteDataReaderreader=mycommand.ExecuteReader();dt.Load;reader.Close();cnn.Close(捕获{thrownewException;}returndt;}//////允许程序员出于查询以外的目的与数据库交互{returnvalue.ToString();}return“”;}//////允许程序员轻松更新数据库中的行。

1.数据库转换成db文件(百度搜索:SQLite转对文件下载sql server to sqlite db converter工具)

SQLite使用第1张

2.添加引用 System.Data.SQLite

3.SQLite DBhelper类

4.写语句直接调用helper类(话不多说,直接上源码)

String dbConnection;

/// <summary>
/// SQLiteDatabase类的默认构造函数。
/// </summary>
public SQLiteDatabase()
{
dbConnection = @"Data Source=D:垃圾WindowsFormsApp2WindowsFormsApp2inDebug ext.db;Initial Catalog=sqlite;Integrated Security=True;Max Pool Size=10";
}

/// <summary>
/// 用于指定DB文件的单个Param构造函数。
/// </summary>
/// <param name="inputFile">包含数据库的文件</param>
public SQLiteDatabase(String inputFile)
{
dbConnection = String.Format("Data Source={0}", inputFile);
}

/// <summary>
/// 用于指定高级连接选项的单个Param构造函数。
/// </summary>
/// <param name="connectionOpts">包含所有所需选项及其值的字典</param>
public SQLiteDatabase(Dictionary<String, String> connectionOpts)
{
String str = "";
foreach (KeyValuePair<String, String> row in connectionOpts)
{
str += String.Format("{0}={1}; ", row.Key, row.Value);
}
str = str.Trim().Substring(0, str.Length - 1);
dbConnection = str;
}

/// <summary>
/// 允许程序员对数据库运行查询。
/// </summary>
/// <param name="sql">The SQL to run</param>
/// <returns>A DataTable containing the result set.</returns>
public DataTable GetDataTable(string sql)
{
DataTable dt = new DataTable();
try
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
SQLiteDataReader reader = mycommand.ExecuteReader();
dt.Load(reader);
reader.Close();
cnn.Close();
}
catch (Exception e)
{
throw new Exception(e.Message);
}
return dt;
}

/// <summary>
/// 允许程序员为查询以外的目的与数据库进行交互。
/// </summary>
/// <param name="sql">The SQL to be run.</param>
/// <returns>An Integer containing the number of rows updated.</returns>
public int ExecuteNonQuery(string sql)
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
int rowsUpdated = mycommand.ExecuteNonQuery();
cnn.Close();
return rowsUpdated;
}

/// <summary>
/// 允许程序员从DB检索单个项。
/// </summary>
public string ExecuteScalar(string sql)
{
SQLiteConnection cnn = new SQLiteConnection(dbConnection);
cnn.Open();
SQLiteCommand mycommand = new SQLiteCommand(cnn);
mycommand.CommandText = sql;
object value = mycommand.ExecuteScalar();
cnn.Close();
if (value != null)
{
return value.ToString();
}
return "";
}

/// <summary>
/// 允许程序员轻松地更新DB中的行。
/// </summary>
public bool Update(String tableName, Dictionary<String, String> data, String where)
{
String vals = "";
Boolean returnCode = true;
if (data.Count >= 1)
{
foreach (KeyValuePair<String, String> val in data)
{
vals += String.Format(" {0} = '{1}',", val.Key.ToString(), val.Value.ToString());
}
vals = vals.Substring(0, vals.Length - 1);
}
try
{
this.ExecuteNonQuery(String.Format("update {0} set {1} where {2};", tableName, vals, where));
}
catch
{
returnCode = false;
}
return returnCode;
}
/// <summary>
/// 允许程序员轻松地从DB中删除行。
/// </summary>
public bool Delete(String tableName, String where)
{
Boolean returnCode = true;
try
{
this.ExecuteNonQuery(String.Format("delete from {0} where {1};", tableName, where));
}
catch (Exception fail)
{
Console.WriteLine(fail.Message);
returnCode = false;
}
return returnCode;
}

/// <summary>
/// 允许程序员很容易地插入到DB中。
/// </summary>
public bool Insert(String tableName, Dictionary<String, String> data)
{
String columns = "";
String values = "";
Boolean returnCode = true;
foreach (KeyValuePair<String, String> val in data)
{
columns += String.Format(" {0},", val.Key.Trim().ToString());
values += String.Format(" '{0}',", val.Value);
}
columns = columns.Substring(0, columns.Length - 1);
values = values.Substring(0, values.Length - 1);
try
{
this.ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
}
catch (Exception fail)
{
Console.WriteLine(fail.Message);
returnCode = false;
}
return returnCode;
}

/// <summary>
/// 允许程序员轻松地从DB中删除所有数据。
/// </summary>
public bool ClearDB()
{
DataTable tables;
try
{
tables = this.GetDataTable("select NAME from SQLITE_MASTER where type='table' order by NAME;");
foreach (DataRow table in tables.Rows)
{
this.ClearTable(table["NAME"].ToString());
}
return true;
}
catch
{
return false;
}
}

/// <summary>
///清空表所有数据
/// </summary>

public bool ClearTable(String table)
{
try
{

this.ExecuteNonQuery(String.Format("delete from {0};", table));
return true;
}
catch
{
return false;
}
}

免责声明:文章转载自《SQLite使用》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇UFPS入门: Unity FPS 教程C++的STL中accumulate的用法下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

浅谈利用SQLite存储离散瓦片的思路和实现方法

文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1.背景 在多个项目中涉及到互联网地图的内网显示,通过自制工具完成了互联网地图的瓦片下载。但是此种方法存在如下几个问题: a.瓦片均是离散型图片,远程部署非常耗时。 b.瓦片下载中,涉及到将互联网瓦片下载至内存,然后建立对应文件夹...

如何利用C#代码获取SQLite数据库的元数据

Sqlite数据库,在很多场合已经用得比较多,由于我的代码生成工具的需要,需要把Sqlite的表、字段、视图等信息获取出来,以便实现各种数据库快速生成项目工程的操作。这里就需要利用C#获取Sqlite数据库的元数据了,和其他数据库一样。 为了获取Sqlite的数据库对象数据,我做了一个测试的例子来获取他的相关信息,其实它的元数据还是和Access的操作方式...

.NET作品集:linux下的博客程序

博客程序架构 本博客程序是博主11年的时候参考loachs小泥鳅博客内核开发的.net跨平台博客cms,距今已有6年多了,个人博客网站一直在用,虽然没有wordpress那么强大,但是当时在深究.net的同时,自己能写一个博客程序,并且基于独立Linux服务器搭建一个自己的.net网站还是挺有意思,毕竟当年运行在linux下的.net网站还是极少数的,之前的...

Kafka监控工具——Kafka-Eagle

Kafka监控工具 官网 https://www.kafka-eagle.org/ 是什么 Kafka Eagle是一款用于监控和管理Apache Kafka的完全开源系统,目前托管在Github,由笔者和一些开源爱好者共同维护。它提供了完善的管理页面,很方面的去管理和可视化Kafka集群的一些信息,例如Broker详情、性能指标趋势、Topic集合、消费...

Android类参考---SQLiteOpenHelper

public 抽象类 SQLiteOpenHelper 继承关系 java.lang.Object |____android.database.sqlite.SQLiteOpenHelper 类概要 这是一个辅助类,用来管理数据库的创建和数据库的版本。 你要创建一个这个类的子类来实现onCreate(SQLiteDatabase),onUpgrade(SQ...

iOS学习——iOS常用的存储方式

不管是在iOS还是Android开发过程中,我们都经常性地需要存储一些状态和数据,比如用户对于App的相关设置、需要在本地缓存的数据等等。根据要存储的的数据的大小、存储性质以及存储类型,在iOS和Android中哪个都有多种存储方式。其中,iOS中的存储方式主要包括以下六类: plist文件(属性列表) preference(偏好设置) NSKeyedA...