合并msg数据库

This commit is contained in:
shuaikangzhou 2023-11-29 21:23:44 +08:00
parent 14298c1b23
commit 844eb9317a
5 changed files with 95 additions and 63 deletions

52
app/DataBase/merge.py Normal file
View File

@ -0,0 +1,52 @@
import os
import sqlite3
def merge_databases(source_paths, target_path):
# 创建目标数据库连接
target_conn = sqlite3.connect(target_path)
target_cursor = target_conn.cursor()
try:
# 开始事务
target_conn.execute("BEGIN;")
for i, source_path in enumerate(source_paths):
if not os.path.exists(source_path):
break
db = sqlite3.connect(source_path)
cursor = db.cursor()
sql = '''
SELECT TalkerId,MsgsvrID,Type,SubType,IsSender,CreateTime,Sequence,StrTalker,StrContent,DisplayContent
FROM MSG;
'''
cursor.execute(sql)
result = cursor.fetchall()
# 附加源数据库
target_cursor.executemany(
"INSERT INTO MSG (TalkerId,MsgsvrID,Type,SubType,IsSender,CreateTime,Sequence,StrTalker,StrContent,DisplayContent) VALUES(?,?,?,?,?,?,?,?,?,?)",
result)
cursor.close()
db.close()
# 提交事务
target_conn.execute("COMMIT;")
except Exception as e:
# 发生异常时回滚事务
target_conn.execute("ROLLBACK;")
raise e
finally:
# 关闭目标数据库连接
target_conn.close()
if __name__ == "__main__":
# 源数据库文件列表
source_databases = ["Msg/MSG1.db", "Msg/MSG2.db", "Msg/MSG3.db"]
# 目标数据库文件
target_database = "Msg/MSG.db"
import shutil
shutil.copy('Msg/MSG0.db', target_database) # 使用一个数据库文件作为模板
# 合并数据库
merge_databases(source_databases, target_database)

View File

@ -5,10 +5,10 @@ import threading
lock = threading.Lock()
DB = None
cursor = None
misc_path = "./app/Database/Msg/Misc.db"
db_path = "./app/Database/Msg/Misc.db"
# misc_path = './Msg/Misc.db'
if os.path.exists(misc_path):
DB = sqlite3.connect(misc_path, check_same_thread=False)
if os.path.exists(db_path):
DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
cursor = DB.cursor()
@ -17,8 +17,8 @@ def init_database():
global DB
global cursor
if not DB:
if os.path.exists(misc_path):
DB = sqlite3.connect(misc_path, check_same_thread=False)
if os.path.exists(db_path):
DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
cursor = DB.cursor()

View File

@ -1,49 +1,32 @@
import os.path
import re
import sqlite3
import threading
from pprint import pprint
DB = []
cursor = []
msg_root_path = "./app/Database/Msg/"
DB = None
cursor = None
db_path = "./app/Database/Msg/MSG.db"
lock = threading.Lock()
# misc_path = './Msg/Misc.db'
if os.path.exists(msg_root_path):
for root, dirs, files in os.walk(msg_root_path):
for file in files:
if re.match('^MSG[0-9]+\.db$', file):
# print('ok', file)
msg_path = os.path.join(msg_root_path, file)
DB0 = sqlite3.connect(msg_path, check_same_thread=False)
# '''创建游标'''
cursor0 = DB0.cursor()
DB.append(DB0)
cursor.append(cursor0)
if os.path.exists(db_path):
DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
cursor = DB.cursor()
def is_database_exist():
return os.path.exists(msg_root_path + 'MSG0.db')
return os.path.exists(db_path)
def init_database():
global DB
global cursor
print(DB)
if not DB:
if os.path.exists(msg_root_path):
for root, dirs, files in os.walk(msg_root_path):
for file in files:
# print(file)
if re.match('^MSG[0-9]+\.db$', file):
print('ok', file)
msg_path = os.path.join(msg_root_path, file)
DB0 = sqlite3.connect(msg_path, check_same_thread=False)
# '''创建游标'''
cursor0 = DB0.cursor()
DB.append(DB0)
cursor.append(cursor0)
if os.path.exists(db_path):
DB = sqlite3.connect(db_path, check_same_thread=False)
# '''创建游标'''
cursor = DB.cursor()
def get_messages(username_):
@ -53,16 +36,12 @@ def get_messages(username_):
where StrTalker=?
order by CreateTime
'''
result = []
for cur in cursor:
try:
lock.acquire(True)
cur.execute(sql, [username_])
result_ = cur.fetchall()
# print(len(result))
result += result_
finally:
lock.release()
try:
lock.acquire(True)
cursor.execute(sql, [username_])
result = cursor.fetchall()
finally:
lock.release()
result.sort(key=lambda x: x[5])
return result
@ -73,16 +52,12 @@ def get_messages_all():
from MSG
order by CreateTime
'''
result = []
for cur in cursor:
try:
lock.acquire(True)
cur.execute(sql)
result_ = cur.fetchall()
# print(len(result))
result += result_
finally:
lock.release()
try:
lock.acquire(True)
cursor.execute(sql)
result = cursor.fetchall()
finally:
lock.release()
result.sort(key=lambda x: x[5])
return result
@ -95,18 +70,13 @@ def get_message_by_num(username_, local_id):
order by CreateTime desc
limit 10
'''
result = []
try:
lock.acquire(True)
for cur in cursor:
cur = cursor[-1]
cur.execute(sql, [username_, local_id])
result_ = cur.fetchall()
result += result_
return result_
cursor.execute(sql, [username_, local_id])
result = cursor.fetchall()
finally:
lock.release()
result.sort(key=lambda x: x[5])
# result.sort(key=lambda x: x[5])
return result

View File

@ -30,6 +30,7 @@ def log(func):
try:
return func(*args, **kwargs)
except Exception as e:
logger.error(f"\n{func.__qualname__} is error,here are details:\n{traceback.format_exc()}")
logger.error(
f"\n{func.__qualname__} is error,params:{(args, kwargs)},here are details:\n{traceback.format_exc()}")
return log_

View File

@ -7,6 +7,7 @@ from PyQt5.QtCore import pyqtSignal, QThread, QUrl, QFile, QIODevice, QTextStrea
from PyQt5.QtGui import QDesktopServices
from PyQt5.QtWidgets import QWidget, QMessageBox, QFileDialog
from app.DataBase.merge import merge_databases
from app.decrypt import get_wx_info, decrypt
from app.log import logger
from . import decryptUi
@ -157,6 +158,14 @@ class DecryptControl(QWidget, decryptUi.Ui_Dialog):
except:
with open('./info.json', 'w', encoding='utf-8') as f:
f.write(json.dumps(dic))
# 目标数据库文件
target_database = "app/DataBase/Msg/MSG.db"
# 源数据库文件列表
source_databases = [f"app/DataBase/Msg/MSG{i}.db" for i in range(20)]
import shutil
shutil.copy("app/DataBase/Msg/MSG0.db", target_database) # 使用一个数据库文件作为模板
# 合并数据库
merge_databases(source_databases, target_database)
self.DecryptSignal.emit(True)
self.close()