什么是SQLite?
SQLite是一个进程内的库,实现了自给自足的、无服务器的、零配置的、事务性的 SQL 数据库引擎。(简单来说就是免安装,只有一个.db文件,在客户电脑部署环境,终于不用再单独安装数据库啦!)
为什么要用 SQLite?
不需要一个单独的服务器进程或操作的系统(无服务器的)。
SQLite 不需要配置,这意味着不需要安装或管理。
一个完整的 SQLite 数据库是存储在一个单一的跨平台的磁盘文件。
SQLite 是非常小的,是轻量级的,完全配置时小于 400KiB,省略可选功能配置时小于250KiB。
SQLite 是自给自足的,这意味着不需要任何外部的依赖。
SQLite 事务是完全兼容 ACID 的,允许从多个进程或线程安全访问。
SQLite 支持 SQL92(SQL2)标准的大多数查询语言的功能。
SQLite 使用 ANSI-C 编写的,并提供了简单和易于使用的 API。
SQLite 可在 UNIX(Linux, Mac OS-X, Android, iOS)和 Windows(Win32, WinCE, WinRT)中运行。
性能比较
我这里对于写操作的性能要求很高,同时对于稳定性要求更高,所以关闭了日志功能和崩溃自动回滚功能。(关掉后效率能翻至少好几倍)
发现对于单机环境下,单线程模式下,sqlite的效率是mySQL的两倍左右!
增删改查案例
闲来无事,做了一个SQLite3的增删改查Demo~
项目中包含辅助类,非常好用。可以直接Copy到自己的项目中,按照项目中的操作方法直接操作。
辅助类封装了所有方法,用的时候直接调一下就好,非常方便(PS:性能也很好哟,大数据量也不虚)
查
/// <summary>
/// 查询数据
/// </summary>
private void QueryData()
{
try
{
string sql = "select * from Cus_ExcelModel_Master ";
DataSet dataSet = SQLiteHelpers.ExecuteDataSet(sql);
if (dataSet != null)
{
this.dg_master.DataSource = dataSet.Tables[0];
}
}
catch (Exception ex)
{
MessageBox.Show($"读取失败\r\n{ex.ToString()}");
}
}
增
/// <summary>
/// 添加数据
/// </summary>
private void AddData()
{
try
{
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("id", Guid.NewGuid().ToString());
dic.Add("modelname", this.txt_modelname.Text.ToString());
dic.Add("tablename", this.txt_tablename.Text.ToString());
dic.Add("ywlx", this.txt_ywlx.Text.ToString());
dic.Add("filepath", this.txt_filepath.Text.ToString());
dic.Add("blzd1", this.txt_blzd1.Text.ToString());
dic.Add("blzd2", this.txt_blzd2.Text.ToString());
dic.Add("blzd3", this.txt_blzd3.Text.ToString());
dic.Add("blzd4", this.txt_blzd4.Text.ToString());
int result = SQLiteHelpers.InsertData("Cus_ExcelModel_Master ", dic);
MessageBox.Show("插入成功,受影响的行数:" + result);
}
catch (Exception ex)
{
MessageBox.Show($"插入失败\r\n{ex.ToString()}");
}
}
删
/// <summary>
/// 删除数据
/// </summary>
private void DeleteData()
{
try
{
if (string.IsNullOrEmpty(this.txt_id.Text.ToString()))
{
MessageBox.Show("要先选择一条数据");
return;
}
//where条件
string where = "ID = @ID";
//where条件中对应的参数
SQLiteParameter[] parameter = new SQLiteParameter[]
{
new SQLiteParameter("ID", this.txt_id.Text.ToString()),
};
int result = SQLiteHelpers.Delete("Cus_ExcelModel_Master", where, parameter);
MessageBox.Show("删除结果,受影响的行数:" + result);
}
catch (Exception ex)
{
MessageBox.Show($"删除失败\r\n{ex.ToString()}");
}
}
改
/// <summary>
/// 更新数据
/// </summary>
private void UpdateData()
{
try
{
if (string.IsNullOrEmpty(this.txt_id.Text.ToString()))
{
MessageBox.Show("要先选择一条数据");
return;
}
//不一定要修改全部列(此处是修改全部)
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("modelname", this.txt_modelname.Text.ToString());
dic.Add("tablename", this.txt_tablename.Text.ToString());
dic.Add("ywlx", this.txt_ywlx.Text.ToString());
dic.Add("filepath", this.txt_filepath.Text.ToString());
dic.Add("blzd1", this.txt_blzd1.Text.ToString());
dic.Add("blzd2", this.txt_blzd2.Text.ToString());
dic.Add("blzd3", this.txt_blzd3.Text.ToString());
dic.Add("blzd4", this.txt_blzd4.Text.ToString());
//where条件
string where = "ID = @ID";
//string where = "ID = @ID AND age = @Age";
//where条件中对应的参数
SQLiteParameter[] parameter = new SQLiteParameter[]
{
new SQLiteParameter("ID", this.txt_id.Text.ToString())
//,new SQLiteParameter("Age",23)
};
int result = SQLiteHelpers.Update("Cus_ExcelModel_Master ", dic, where, parameter);
MessageBox.Show("修改结果,受影响的行数:" + result);
}
catch (Exception ex)
{
MessageBox.Show($"修改失败\r\n{ex.ToString()}");
}
}
项目源码及数据库文件分享给大家,帮助大家在使用Sqlite3的道路上少走弯路!