一、安装:
1、引用NuGet包:
2、sqlite-net-pcl的“数据库连接类”与“数据库配置文件”;
① Sqilte帮助类-单例
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:Sqilte帮助类-单例
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2022-11-20 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Repository._Base
*│ 类 名:SQLiteDBBase(PracticeContext)
*└──────────────────────────────────────────────────────────────┘
*/
using SQLite;
using System.Diagnostics;
namespace Repository._Base
{
/// <summary>
/// MAUI的Sqilte帮助类
/// </summary>
public class SQLiteDBBase
{
/// <summary>
/// 数据库连接操作对象
/// </summary>
internal static SQLiteAsyncConnection _SQLiteDBConnection;
public SQLiteDBBase()
{
}
/// <summary>
/// 初始化连接
/// </summary>
public void Init()
{
if(SQLiteDBBase._SQLiteDBConnection is null)
{
SQLiteDBBase._SQLiteDBConnection = new SQLiteAsyncConnection(SQLiteDBConfg.DatabasePath, SQLiteDBConfg.Flags);
}
//var result = await Database.CreateTableAsync<TodoItem>();
}
/// <summary>
/// 获取SQLite数据库文件路径
/// </summary>
/// <returns></returns>
public string GetDatabaseFilePath()
{
return SQLiteDBBase._SQLiteDBConnection.DatabasePath;
}
/// <summary>
/// 获取SQLite的Lib版本
/// </summary>
/// <returns></returns>
public int GetLibVersionNumber()
{
return SQLiteDBBase._SQLiteDBConnection.LibVersionNumber;
}
/// <summary>
/// 获取SQLite的DateTimeString格式
/// </summary>
/// <returns></returns>
public string GetDateTimeStringFormat()
{
Debug.WriteLine(SQLiteDBBase._SQLiteDBConnection.DateTimeStringFormat);
return SQLiteDBBase._SQLiteDBConnection.DateTimeStringFormat;
}
/// <summary>
/// 获取SQLite的TableMappings
/// </summary>
/// <returns></returns>
public List<TableMapping> GetTableMappings()
{
return SQLiteDBBase._SQLiteDBConnection.TableMappings.ToList();
}
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
② SQLite配置数据
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:SQLite配置数据
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2022-11-20 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Repository._Base
*│ 类 名:SQLiteDBConfg
*└──────────────────────────────────────────────────────────────┘
*/
using SQLite;
using System.Diagnostics;
namespace Repository._Base
{
/// <summary>
/// SQLite配置数据
/// </summary>
internal class SQLiteDBConfg
{
/// <summary>
/// SQLite文件名
/// </summary>
private const string DatabaseFilename = "MengFeiYue.db3";
/// <summary>
/// SQLite路径
/// </summary>
internal static string DatabasePath => Path.Combine(FileSystem.AppDataDirectory, DatabaseFilename);
/// <summary>
/// SQLite连接配置
/// </summary>
internal static readonly SQLite.SQLiteOpenFlags Flags =
SQLite.SQLiteOpenFlags.Create | // 如果连接不存在,连接将自动创建数据库文件
SQLite.SQLiteOpenFlags.ReadWrite | // 连接可以读取和写入数据。
//SQLite.SQLiteOpenFlags.FullMutex | // 连接在序列化线程模式下打开。
SQLite.SQLiteOpenFlags.NoMutex | // 连接在多线程模式下打开。
SQLite.SQLiteOpenFlags.SharedCache | // 如果连接已启用,则连接将参与共享缓存。
//SQLite.SQLiteOpenFlags.PrivateCache | // 即使连接已启用,连接也不会参与共享缓存。
SQLite.SQLiteOpenFlags.ProtectionNone // 数据库文件未加密。
//SQLite.SQLiteOpenFlags.ProtectionComplete // 设备锁定时,文件已加密且不可访问。
//SQLite.SQLiteOpenFlags.ProtectionCompleteUntilFirstUserAuthentication // 文件已加密,直到用户启动并解锁设备。
//SQLite.SQLiteOpenFlags.ProtectionCompleteUnlessOpen // 文件已加密,直到它打开,但即使用户锁定设备,也可以访问该文件。
;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
二、sqlite-net-pcl使用教程:
1、sqlite-net-pcl特征讲解
SQLite实体示例
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:SQLite实体示例
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2022-11-20 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Model.Test
*│ 类 名:TableTest
*└──────────────────────────────────────────────────────────────┘
*/
using SQLite;
namespace Model.Test
{
/// <summary>
/// 测试表
/// </summary>
[Table("tableTest")]
public class TableTest
{
public TableTest() { }
/// <summary>
/// 主键,自增,唯一
/// </summary>
[PrimaryKey, AutoIncrement, Unique]
public int Id { get; set; }
/// <summary>
/// 索引,
/// </summary>
[Indexed]
public string Name { get; set; } = string.Empty;
/// <summary>
/// 列名,限制文本长度
/// </summary>
[Column("pwd"), MaxLength(12)]
public string Pwd { get; set; } = string.Empty;
public string Description { get; set; } = string.Empty;
/// <summary>
/// Ignore:忽略此属性加入ORM
/// </summary>
[Ignore]
public string Test { get; set; } = string.Empty;
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
2、增删改查等常见示例
/**
*┌──────────────────────────────────────────────────────────────┐
*│ 描 述:SQLite数据操作示例
*│ 作 者:执笔小白
*│ 版 本:1.0
*│ 创建时间:2022-11-20 15:40:56
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│ 命名空间: Repository._Base
*│ 类 名:SQLiteDBDemo
*└──────────────────────────────────────────────────────────────┘
*/
using Model.Test;
using SQLite;
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace Repository._Base
{
/// <summary>
/// SQLite数据操作示例
/// </summary>
public class SQLiteDBDemo:SQLiteDBBase
{
#region sql执行
public void RunSQL(){
_SQLiteDBConnection.ExecuteAsync("create table Stock(Symbol varchar(100) not null)");
_SQLiteDBConnection.ExecuteAsync("insert into Stock(Symbol) values (?)", "MSFT");
var stocks = _SQLiteDBConnection.QueryScalarsAsync<TableTest>("select * from Stock");
}
#endregion sql执行
#region 添加表数据
/// <summary>
/// 新增数据
/// </summary>
/// <param name="tableTest">数据</param>
/// <returns></returns>
public int InsertTableDataDemo(TableTest tableTest)
{
return _SQLiteDBConnection.InsertAsync(tableTest).Result;
}
/// <summary>
/// 新增多条表数据
/// </summary>
/// <param name="tableTests">数据</param>
/// <param name="runInTransaction">是否使用事务</param>
/// <returns></returns>
public int InsertTableDatasDemo(List<TableTest> tableTests, bool runInTransaction = true)
{
return _SQLiteDBConnection.InsertAllAsync(tableTests, runInTransaction).Result;
}
#endregion 添加表数据
#region 删除表数据
/// <summary>
/// 删除数据
/// </summary>
/// <param name="tableTest"></param>
/// <returns></returns>
public int DeleteTableDataDemo(TableTest tableTest)
{
return _SQLiteDBConnection.DeleteAsync(tableTest).Result;
}
#endregion 删除表数据
#region 修改表数据
/// <summary>
/// 更新数据
/// </summary>
/// <param name="tableTest">数据</param>
/// <returns></returns>
public int UpdateTableDataDemo(TableTest tableTest)
{
return _SQLiteDBConnection.UpdateAsync(tableTest).Result;
}
/// <summary>
/// 更新所有的表数据
/// </summary>
/// <param name="tableTests">数据</param>
/// <param name="runInTransaction">是否使用事务</param>
/// <returns></returns>
public int UpdateAllTableDataDemo(List<TableTest> tableTests,bool runInTransaction = true)
{
return _SQLiteDBConnection.UpdateAllAsync(tableTests, runInTransaction).Result;
}
#endregion 修改表数据
#region 查询表数据
/// <summary>
/// 查询
/// </summary>
/// <param name="tableTest">条件</param>
/// <returns></returns>
public List<TableTest> QueryTableDataByWhere(TableTest tableTest)
{
return _SQLiteDBConnection.QueryAsync<TableTest>("select * from TableTest where Name=? and Pwd=?", tableTest.Name, tableTest.Pwd).Result;
}
/// <summary>
/// 通过Id获取一条数据
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
public TableTest GetTableDataById(int id)
{
return _SQLiteDBConnection.QueryAsync<TableTest>("select * from TableTest where Id=?", id).Result.FirstOrDefault();
}
/// <summary>
/// 查询全部的数据
/// 返回一个可供查询的表,后筛选
/// </summary>
/// <param name="tableTest">条件</param>
/// <returns></returns>
public List<TableTest> GetTableDataByWhere(TableTest tableTest=null)
{
if (tableTest !=null)
{
return _SQLiteDBConnection.Table<TableTest>().Where(t => t.Name == tableTest.Name).ToListAsync().Result;
}
else
{
return _SQLiteDBConnection.Table<TableTest>().ToListAsync().Result;
}
}
#endregion 查询表数据
#region 事务
// 执行事务
public void RunInTransactionAsync()
{
// 操作
TableTest insertObj = new();
TableTest updateObj = new();
TableTest deleteObj = new();
_SQLiteDBConnection.RunInTransactionAsync(tran =>
{
// database calls inside the transaction
tran.Insert(insertObj);
tran.Update(updateObj);
tran.Delete(deleteObj);
});
}
#endregion 事务
#region 创建表
/// <summary>
/// 创建表
/// </summary>
public void CtreatTableDemo()
{
_SQLiteDBConnection.CreateTableAsync<TableTest>();
}
#endregion 创建表
#region 删除表
/// <summary>
/// 删除表
/// </summary>
public void DropTableDemo()
{
_SQLiteDBConnection.DropTableAsync<TableTest>();
}
#endregion 删除表
#region 添加列或修改列、删除列结构;添加、删除索引
/// <summary>
/// 添加列或修改列结构
/// </summary>
public void ModifyColumnsForTableDemo()
{
_SQLiteDBConnection.CreateTableAsync<TableTest>();
}
#endregion 添加列或修改列、删除列结构;
#region 添加、删除索引(可以用特征指定)
/// <summary>
/// 创建索引
/// </summary>
public int CreateIndexAsync()
{
TableTest tableTest = new();
string indexName = "TTName_Index"; // 索引名
bool unique = false; // 是否唯一索引
//return _SQLiteDBConnection.CreateIndexAsync(nameof(TableTest), nameof(tableTest.Name), unique).Result; // 单个列索引
return _SQLiteDBConnection.CreateIndexAsync(indexName, nameof(TableTest), nameof(tableTest.Name), unique).Result; // 单个列索引_指定索引名
//return _SQLiteDBConnection.CreateIndexAsync(nameof(TableTest),new string[] { nameof(tableTest.Name), nameof(tableTest.Pwd), nameof(tableTest.Description) }, unique).Result; // 多个列索引
}
#endregion 添加、删除索引(可以用特征指定)
}
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
- 57.
- 58.
- 59.
- 60.
- 61.
- 62.
- 63.
- 64.
- 65.
- 66.
- 67.
- 68.
- 69.
- 70.
- 71.
- 72.
- 73.
- 74.
- 75.
- 76.
- 77.
- 78.
- 79.
- 80.
- 81.
- 82.
- 83.
- 84.
- 85.
- 86.
- 87.
- 88.
- 89.
- 90.
- 91.
- 92.
- 93.
- 94.
- 95.
- 96.
- 97.
- 98.
- 99.
- 100.
- 101.
- 102.
- 103.
- 104.
- 105.
- 106.
- 107.
- 108.
- 109.
- 110.
- 111.
- 112.
- 113.
- 114.
- 115.
- 116.
- 117.
- 118.
- 119.
- 120.
- 121.
- 122.
- 123.
- 124.
- 125.
- 126.
- 127.
- 128.
- 129.
- 130.
- 131.
- 132.
- 133.
- 134.
- 135.
- 136.
- 137.
- 138.
- 139.
- 140.
- 141.
- 142.
- 143.
- 144.
- 145.
- 146.
- 147.
- 148.
- 149.
- 150.
- 151.
- 152.
- 153.
- 154.
- 155.
- 156.
- 157.
- 158.
- 159.
- 160.
- 161.
- 162.
- 163.
- 164.
- 165.
- 166.
- 167.
- 168.
- 169.
- 170.
- 171.
- 172.
- 173.
- 174.
- 175.
- 176.
- 177.
- 178.
- 179.
- 180.
- 181.
- 182.
- 183.
- 184.
- 185.
- 186.
- 187.
- 188.
- 189.
- 190.
- 191.
- 192.
- 193.
- 194.
- 195.
- 196.
- 197.
- 198.
- 199.
- 200.
- 201.
- 202.
- 203.
作者:꧁执笔小白꧂