using DownKyi.Core.Logging; using System; using System.Collections.Generic; using System.Data.SQLite; namespace DownKyi.Core.Storage.Database { public class DbHelper { private readonly string connStr; private readonly SQLiteConnection conn; private static readonly Dictionary database = new Dictionary(); /// /// 创建一个数据库 /// /// public DbHelper(string dbPath) { connStr = $"Data Source={dbPath};Version=3;"; if (database.ContainsKey(connStr)) { conn = database[connStr]; if (conn != null) { return; } } conn = new SQLiteConnection(connStr); database.Add(connStr, conn); } /// /// 创建一个带密码的数据库 /// /// /// public DbHelper(string dbPath, string secretKey) { connStr = $"Data Source={dbPath};Version=3;"; if (database.ContainsKey(connStr)) { conn = database[connStr]; if (conn != null) { return; } } conn = new SQLiteConnection(connStr); conn.SetPassword(secretKey); database.Add(connStr, conn); } /// /// 连接是否开启 /// /// public bool IsOpen() { return conn.State == System.Data.ConnectionState.Open; } /// /// 开启连接 /// public void Open() { if (conn == null) { return; } if (!IsOpen()) { conn.Open(); } } /// /// 关闭数据库 /// public void Close() { if (conn == null) { return; } if (IsOpen()) { conn.Close(); database.Remove(connStr); } } /// /// 执行一条SQL语句 /// /// public void ExecuteNonQuery(string sql, Action action = null) { if (conn == null) { return; } try { lock (conn) { Open(); using (var tr = conn.BeginTransaction()) { using (var command = conn.CreateCommand()) { command.CommandText = sql; // 添加参数 action?.Invoke(command.Parameters); command.ExecuteNonQuery(); } tr.Commit(); } } } catch (SQLiteException e) { Utils.Debugging.Console.PrintLine("DbHelper ExecuteNonQuery()发生异常: {0}", e); LogManager.Error("DbHelper ExecuteNonQuery()", e); } } /// /// 执行一条SQL语句,并执行提供的操作,一般用于查询 /// /// /// public void ExecuteQuery(string sql, Action action) { if (conn == null) { return; } try { lock (conn) { Open(); using (var command = conn.CreateCommand()) { command.CommandText = sql; var reader = command.ExecuteReader(); action(reader); } } } catch (SQLiteException e) { Utils.Debugging.Console.PrintLine("DbHelper ExecuteQuery()发生异常: {0}", e); LogManager.Error("DbHelper ExecuteQuery()", e); } } } }