mirror of
https://github.com/LC044/WeChatMsg
synced 2024-11-14 22:01:54 +08:00
合并msg数据库
This commit is contained in:
parent
14298c1b23
commit
844eb9317a
52
app/DataBase/merge.py
Normal file
52
app/DataBase/merge.py
Normal 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)
|
@ -5,10 +5,10 @@ import threading
|
|||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
DB = None
|
DB = None
|
||||||
cursor = None
|
cursor = None
|
||||||
misc_path = "./app/Database/Msg/Misc.db"
|
db_path = "./app/Database/Msg/Misc.db"
|
||||||
# misc_path = './Msg/Misc.db'
|
# misc_path = './Msg/Misc.db'
|
||||||
if os.path.exists(misc_path):
|
if os.path.exists(db_path):
|
||||||
DB = sqlite3.connect(misc_path, check_same_thread=False)
|
DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||||
# '''创建游标'''
|
# '''创建游标'''
|
||||||
cursor = DB.cursor()
|
cursor = DB.cursor()
|
||||||
|
|
||||||
@ -17,8 +17,8 @@ def init_database():
|
|||||||
global DB
|
global DB
|
||||||
global cursor
|
global cursor
|
||||||
if not DB:
|
if not DB:
|
||||||
if os.path.exists(misc_path):
|
if os.path.exists(db_path):
|
||||||
DB = sqlite3.connect(misc_path, check_same_thread=False)
|
DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||||
# '''创建游标'''
|
# '''创建游标'''
|
||||||
cursor = DB.cursor()
|
cursor = DB.cursor()
|
||||||
|
|
||||||
|
@ -1,49 +1,32 @@
|
|||||||
import os.path
|
import os.path
|
||||||
import re
|
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import threading
|
import threading
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
DB = []
|
DB = None
|
||||||
cursor = []
|
cursor = None
|
||||||
msg_root_path = "./app/Database/Msg/"
|
db_path = "./app/Database/Msg/MSG.db"
|
||||||
lock = threading.Lock()
|
lock = threading.Lock()
|
||||||
|
|
||||||
# misc_path = './Msg/Misc.db'
|
# misc_path = './Msg/Misc.db'
|
||||||
if os.path.exists(msg_root_path):
|
if os.path.exists(db_path):
|
||||||
for root, dirs, files in os.walk(msg_root_path):
|
DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||||
for file in files:
|
# '''创建游标'''
|
||||||
if re.match('^MSG[0-9]+\.db$', file):
|
cursor = DB.cursor()
|
||||||
# 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)
|
|
||||||
|
|
||||||
|
|
||||||
def is_database_exist():
|
def is_database_exist():
|
||||||
return os.path.exists(msg_root_path + 'MSG0.db')
|
return os.path.exists(db_path)
|
||||||
|
|
||||||
|
|
||||||
def init_database():
|
def init_database():
|
||||||
global DB
|
global DB
|
||||||
global cursor
|
global cursor
|
||||||
print(DB)
|
|
||||||
if not DB:
|
if not DB:
|
||||||
if os.path.exists(msg_root_path):
|
if os.path.exists(db_path):
|
||||||
for root, dirs, files in os.walk(msg_root_path):
|
DB = sqlite3.connect(db_path, check_same_thread=False)
|
||||||
for file in files:
|
# '''创建游标'''
|
||||||
# print(file)
|
cursor = DB.cursor()
|
||||||
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)
|
|
||||||
|
|
||||||
|
|
||||||
def get_messages(username_):
|
def get_messages(username_):
|
||||||
@ -53,16 +36,12 @@ def get_messages(username_):
|
|||||||
where StrTalker=?
|
where StrTalker=?
|
||||||
order by CreateTime
|
order by CreateTime
|
||||||
'''
|
'''
|
||||||
result = []
|
try:
|
||||||
for cur in cursor:
|
lock.acquire(True)
|
||||||
try:
|
cursor.execute(sql, [username_])
|
||||||
lock.acquire(True)
|
result = cursor.fetchall()
|
||||||
cur.execute(sql, [username_])
|
finally:
|
||||||
result_ = cur.fetchall()
|
lock.release()
|
||||||
# print(len(result))
|
|
||||||
result += result_
|
|
||||||
finally:
|
|
||||||
lock.release()
|
|
||||||
result.sort(key=lambda x: x[5])
|
result.sort(key=lambda x: x[5])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -73,16 +52,12 @@ def get_messages_all():
|
|||||||
from MSG
|
from MSG
|
||||||
order by CreateTime
|
order by CreateTime
|
||||||
'''
|
'''
|
||||||
result = []
|
try:
|
||||||
for cur in cursor:
|
lock.acquire(True)
|
||||||
try:
|
cursor.execute(sql)
|
||||||
lock.acquire(True)
|
result = cursor.fetchall()
|
||||||
cur.execute(sql)
|
finally:
|
||||||
result_ = cur.fetchall()
|
lock.release()
|
||||||
# print(len(result))
|
|
||||||
result += result_
|
|
||||||
finally:
|
|
||||||
lock.release()
|
|
||||||
result.sort(key=lambda x: x[5])
|
result.sort(key=lambda x: x[5])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -95,18 +70,13 @@ def get_message_by_num(username_, local_id):
|
|||||||
order by CreateTime desc
|
order by CreateTime desc
|
||||||
limit 10
|
limit 10
|
||||||
'''
|
'''
|
||||||
result = []
|
|
||||||
try:
|
try:
|
||||||
lock.acquire(True)
|
lock.acquire(True)
|
||||||
for cur in cursor:
|
cursor.execute(sql, [username_, local_id])
|
||||||
cur = cursor[-1]
|
result = cursor.fetchall()
|
||||||
cur.execute(sql, [username_, local_id])
|
|
||||||
result_ = cur.fetchall()
|
|
||||||
result += result_
|
|
||||||
return result_
|
|
||||||
finally:
|
finally:
|
||||||
lock.release()
|
lock.release()
|
||||||
result.sort(key=lambda x: x[5])
|
# result.sort(key=lambda x: x[5])
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
|
@ -30,6 +30,7 @@ def log(func):
|
|||||||
try:
|
try:
|
||||||
return func(*args, **kwargs)
|
return func(*args, **kwargs)
|
||||||
except Exception as e:
|
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_
|
return log_
|
||||||
|
@ -7,6 +7,7 @@ from PyQt5.QtCore import pyqtSignal, QThread, QUrl, QFile, QIODevice, QTextStrea
|
|||||||
from PyQt5.QtGui import QDesktopServices
|
from PyQt5.QtGui import QDesktopServices
|
||||||
from PyQt5.QtWidgets import QWidget, QMessageBox, QFileDialog
|
from PyQt5.QtWidgets import QWidget, QMessageBox, QFileDialog
|
||||||
|
|
||||||
|
from app.DataBase.merge import merge_databases
|
||||||
from app.decrypt import get_wx_info, decrypt
|
from app.decrypt import get_wx_info, decrypt
|
||||||
from app.log import logger
|
from app.log import logger
|
||||||
from . import decryptUi
|
from . import decryptUi
|
||||||
@ -157,6 +158,14 @@ class DecryptControl(QWidget, decryptUi.Ui_Dialog):
|
|||||||
except:
|
except:
|
||||||
with open('./info.json', 'w', encoding='utf-8') as f:
|
with open('./info.json', 'w', encoding='utf-8') as f:
|
||||||
f.write(json.dumps(dic))
|
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.DecryptSignal.emit(True)
|
||||||
self.close()
|
self.close()
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user