From 4a3f0d43eb4de5321dd8a26060d20e3dfa9f79e7 Mon Sep 17 00:00:00 2001 From: croire <1432593898@qq.com> Date: Sat, 12 Mar 2022 01:20:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8D=95=E8=8E=B7=E6=95=B0=E6=8D=AE=E5=BA=93?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E7=9A=84=E6=89=80=E6=9C=89=E5=BC=82=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DownKyi.Core/Storage/Database/CoverDb.cs | 33 ++++--- .../Storage/Database/Download/DownloadDb.cs | 99 ++++++++++--------- DownKyi.Core/Storage/Database/HeaderDb.cs | 31 +++--- 3 files changed, 95 insertions(+), 68 deletions(-) diff --git a/DownKyi.Core/Storage/Database/CoverDb.cs b/DownKyi.Core/Storage/Database/CoverDb.cs index 8bca281..1c5ecfa 100644 --- a/DownKyi.Core/Storage/Database/CoverDb.cs +++ b/DownKyi.Core/Storage/Database/CoverDb.cs @@ -103,21 +103,30 @@ namespace DownKyi.Core.Storage.Database { List covers = new List(); - dbHelper.ExecuteQuery(sql, reader => + try { - while (reader.Read()) + dbHelper.ExecuteQuery(sql, reader => { - Cover cover = new Cover + while (reader.Read()) { - Avid = (long)reader["avid"], - Bvid = (string)reader["bvid"], - Cid = (long)reader["cid"], - Url = (string)reader["url"], - Md5 = (string)reader["md5"] - }; - covers.Add(cover); - } - }); + Cover cover = new Cover + { + Avid = (long)reader["avid"], + Bvid = (string)reader["bvid"], + Cid = (long)reader["cid"], + Url = (string)reader["url"], + Md5 = (string)reader["md5"] + }; + covers.Add(cover); + } + }); + } + catch (Exception e) + { + Utils.Debugging.Console.PrintLine("Query()发生异常: {0}", e); + LogManager.Error($"{tableName}", e); + } + return covers; } diff --git a/DownKyi.Core/Storage/Database/Download/DownloadDb.cs b/DownKyi.Core/Storage/Database/Download/DownloadDb.cs index 7d1b0c5..f32c9c9 100644 --- a/DownKyi.Core/Storage/Database/Download/DownloadDb.cs +++ b/DownKyi.Core/Storage/Database/Download/DownloadDb.cs @@ -14,7 +14,7 @@ namespace DownKyi.Core.Storage.Database.Download protected string tableName = "download"; #if DEBUG - private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload().Replace(".db","_debug.db")); + private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload().Replace(".db", "_debug.db")); #else private readonly DbHelper dbHelper = new DbHelper(StorageManager.GetDownload(), key); #endif @@ -33,25 +33,25 @@ namespace DownKyi.Core.Storage.Database.Download /// public void Insert(string uuid, object obj) { - // 定义一个流 - Stream stream = new MemoryStream(); - // 定义一个格式化器 - BinaryFormatter formatter = new BinaryFormatter(); - // 序列化 - formatter.Serialize(stream, obj); + try + { + // 定义一个流 + Stream stream = new MemoryStream(); + // 定义一个格式化器 + BinaryFormatter formatter = new BinaryFormatter(); + // 序列化 + formatter.Serialize(stream, obj); - byte[] array = null; - array = new byte[stream.Length]; + byte[] array = null; + array = new byte[stream.Length]; - //将二进制流写入数组 - stream.Position = 0; - stream.Read(array, 0, (int)stream.Length); + //将二进制流写入数组 + stream.Position = 0; + stream.Read(array, 0, (int)stream.Length); - //关闭流 - stream.Close(); + //关闭流 + stream.Close(); - try - { string sql = $"insert into {tableName}(id, data) values (@id, @data)"; dbHelper.ExecuteNonQuery(sql, new Action((para) => { @@ -86,25 +86,25 @@ namespace DownKyi.Core.Storage.Database.Download public void Update(string uuid, object obj) { - // 定义一个流 - Stream stream = new MemoryStream(); - // 定义一个格式化器 - BinaryFormatter formatter = new BinaryFormatter(); - // 序列化 - formatter.Serialize(stream, obj); + try + { + // 定义一个流 + Stream stream = new MemoryStream(); + // 定义一个格式化器 + BinaryFormatter formatter = new BinaryFormatter(); + // 序列化 + formatter.Serialize(stream, obj); - byte[] array = null; - array = new byte[stream.Length]; + byte[] array = null; + array = new byte[stream.Length]; - //将二进制流写入数组 - stream.Position = 0; - stream.Read(array, 0, (int)stream.Length); + //将二进制流写入数组 + stream.Position = 0; + stream.Read(array, 0, (int)stream.Length); - //关闭流 - stream.Close(); + //关闭流 + stream.Close(); - try - { string sql = $"update {tableName} set data=@data where id glob @id"; dbHelper.ExecuteNonQuery(sql, new Action((para) => { @@ -160,22 +160,31 @@ namespace DownKyi.Core.Storage.Database.Download { Dictionary objects = new Dictionary(); - dbHelper.ExecuteQuery(sql, reader => + try { - while (reader.Read()) + dbHelper.ExecuteQuery(sql, reader => { - // 读取字节数组 - byte[] array = (byte[])reader["data"]; - // 定义一个流 - MemoryStream stream = new MemoryStream(array); - //定义一个格式化器 - BinaryFormatter formatter = new BinaryFormatter(); - // 反序列化 - object obj = formatter.Deserialize(stream); - - objects.Add((string)reader["id"], obj); - } - }); + while (reader.Read()) + { + // 读取字节数组 + byte[] array = (byte[])reader["data"]; + // 定义一个流 + MemoryStream stream = new MemoryStream(array); + //定义一个格式化器 + BinaryFormatter formatter = new BinaryFormatter(); + // 反序列化 + object obj = formatter.Deserialize(stream); + + objects.Add((string)reader["id"], obj); + } + }); + } + catch (Exception e) + { + Utils.Debugging.Console.PrintLine("Query()发生异常: {0}", e); + LogManager.Error($"{tableName}", e); + } + return objects; } diff --git a/DownKyi.Core/Storage/Database/HeaderDb.cs b/DownKyi.Core/Storage/Database/HeaderDb.cs index d1d11c2..325e4a2 100644 --- a/DownKyi.Core/Storage/Database/HeaderDb.cs +++ b/DownKyi.Core/Storage/Database/HeaderDb.cs @@ -90,20 +90,29 @@ namespace DownKyi.Core.Storage.Database { List
headers = new List
(); - dbHelper.ExecuteQuery(sql, reader => + try { - while (reader.Read()) + dbHelper.ExecuteQuery(sql, reader => { - Header header = new Header + while (reader.Read()) { - Mid = (long)reader["mid"], - Name = (string)reader["name"], - Url = (string)reader["url"], - Md5 = (string)reader["md5"] - }; - headers.Add(header); - } - }); + Header header = new Header + { + Mid = (long)reader["mid"], + Name = (string)reader["name"], + Url = (string)reader["url"], + Md5 = (string)reader["md5"] + }; + headers.Add(header); + } + }); + } + catch (Exception e) + { + Utils.Debugging.Console.PrintLine("Query()发生异常: {0}", e); + LogManager.Error($"{tableName}", e); + } + return headers; }