解决同时向数据库插入时冲突的问题

croire 3 years ago
parent 653aff5a02
commit e0f3c978cd

@ -1,4 +1,6 @@
using System.Collections.Generic; using DownKyi.Core.Logging;
using System;
using System.Collections.Generic;
namespace DownKyi.Core.Storage.Database namespace DownKyi.Core.Storage.Database
{ {
@ -26,8 +28,16 @@ namespace DownKyi.Core.Storage.Database
/// <param name="cover"></param> /// <param name="cover"></param>
public void Insert(Cover cover) public void Insert(Cover cover)
{ {
string sql = $"insert into cover values ({cover.Avid}, '{cover.Bvid}', {cover.Cid}, '{cover.Url}', '{cover.Md5}')"; try
dbHelper.ExecuteNonQuery(sql); {
string sql = $"insert into cover values ({cover.Avid}, '{cover.Bvid}', {cover.Cid}, '{cover.Url}', '{cover.Md5}')";
dbHelper.ExecuteNonQuery(sql);
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Insert()发生异常: {0}", e);
LogManager.Error("CoverDb", e);
}
} }
/// <summary> /// <summary>
@ -36,8 +46,17 @@ namespace DownKyi.Core.Storage.Database
/// <param name="cover"></param> /// <param name="cover"></param>
public void Update(Cover cover) public void Update(Cover cover)
{ {
string sql = $"update cover set avid={cover.Avid}, bvid='{cover.Bvid}', cid={cover.Cid}, md5='{cover.Md5}' where url glob '{cover.Url}'"; try
dbHelper.ExecuteNonQuery(sql); {
string sql = $"update cover set avid={cover.Avid}, bvid='{cover.Bvid}', cid={cover.Cid}, md5='{cover.Md5}' where url glob '{cover.Url}'";
dbHelper.ExecuteNonQuery(sql);
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Update()发生异常: {0}", e);
LogManager.Error("CoverDb", e);
}
} }
/// <summary> /// <summary>

@ -1,4 +1,6 @@
using System.Collections.Generic; using DownKyi.Core.Logging;
using System;
using System.Collections.Generic;
namespace DownKyi.Core.Storage.Database namespace DownKyi.Core.Storage.Database
{ {
@ -26,8 +28,16 @@ namespace DownKyi.Core.Storage.Database
/// <param name="header"></param> /// <param name="header"></param>
public void Insert(Header header) public void Insert(Header header)
{ {
string sql = $"insert into header values ({header.Mid}, '{header.Name}', '{header.Url}', '{header.Md5}')"; try
dbHelper.ExecuteNonQuery(sql); {
string sql = $"insert into header values ({header.Mid}, '{header.Name}', '{header.Url}', '{header.Md5}')";
dbHelper.ExecuteNonQuery(sql);
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Insert()发生异常: {0}", e);
LogManager.Error("HeaderDb", e);
}
} }
/// <summary> /// <summary>
@ -36,8 +46,16 @@ namespace DownKyi.Core.Storage.Database
/// <param name="header"></param> /// <param name="header"></param>
public void Update(Header header) public void Update(Header header)
{ {
string sql = $"update header set name='{header.Name}', url='{header.Url}', md5='{header.Md5}' where mid={header.Mid}"; try
dbHelper.ExecuteNonQuery(sql); {
string sql = $"update header set name='{header.Name}', url='{header.Url}', md5='{header.Md5}' where mid={header.Mid}";
dbHelper.ExecuteNonQuery(sql);
}
catch (Exception e)
{
Utils.Debugging.Console.PrintLine("Update()发生异常: {0}", e);
LogManager.Error("HeaderDb", e);
}
} }
/// <summary> /// <summary>
@ -89,7 +107,7 @@ namespace DownKyi.Core.Storage.Database
Mid = (long)reader["mid"], Mid = (long)reader["mid"],
Name = (string)reader["name"], Name = (string)reader["name"],
Url = (string)reader["url"], Url = (string)reader["url"],
Md5=(string)reader["md5"] Md5 = (string)reader["md5"]
}; };
headers.Add(header); headers.Add(header);
} }

@ -161,7 +161,14 @@ namespace DownKyi.Core.Storage
if (File.Exists(localFile)) if (File.Exists(localFile))
{ {
File.Move(localFile, $"{StorageManager.GetCover()}/{md5}"); string destFile = $"{StorageManager.GetCover()}/{md5}";
// 如果不存在
if (!File.Exists(destFile))
{
// 移动到指定位置
File.Move(localFile, destFile);
}
return md5; return md5;
} }

Loading…
Cancel
Save