From 01040f4f789172081a1ebd24671b0412f988c902 Mon Sep 17 00:00:00 2001 From: xaoyaoo Date: Sun, 10 Dec 2023 12:49:24 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dbug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pywxdump/ui/view_chat.py | 3 +++ pywxdump/wx_info/merge_db.py | 47 +++++++++++++++++++++--------------- 2 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pywxdump/ui/view_chat.py b/pywxdump/ui/view_chat.py index 2484d78..265cdf0 100644 --- a/pywxdump/ui/view_chat.py +++ b/pywxdump/ui/view_chat.py @@ -132,6 +132,7 @@ def load_chat_records(selected_talker, start_index, page_size, user_list, MSG_AL img_md5_data = load_base64_img_data(result1[0][7], result1[-1][7], username_md5, FileStorage_path) # 获取图片的base64数据 data = [] + room_username_count = {} for row in result1: localId, IsSender, StrContent, StrTalker, Sequence, Type, SubType, CreateTime, MsgSvrID, DisplayContent, CompressContent, BytesExtra = row CreateTime = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(CreateTime)) @@ -198,6 +199,8 @@ def load_chat_records(selected_talker, start_index, page_size, user_list, MSG_AL talker = matched_string except: pass + else: + talker = user_list.get("remark", user_list.get("nickname", user_list.get("username", ""))) row_data = {"MsgSvrID": MsgSvrID, "type_name": type_name, "is_sender": IsSender, "talker": talker, "content": content, "CreateTime": CreateTime} diff --git a/pywxdump/wx_info/merge_db.py b/pywxdump/wx_info/merge_db.py index 95a8b18..f5b2173 100644 --- a/pywxdump/wx_info/merge_db.py +++ b/pywxdump/wx_info/merge_db.py @@ -197,7 +197,8 @@ def execute_sql(connection, sql, params=None): return cursor.fetchall() -def merge_db(db_paths, save_path="merge.db"): + +def merge_db(db_paths, save_path="merge.db", CreateTime: int = 0): if os.path.isdir(save_path): save_path = os.path.join(save_path, f"merge_{int(time.time())}.db") @@ -209,35 +210,33 @@ def merge_db(db_paths, save_path="merge.db"): else: raise TypeError("db_paths 类型错误") - # 连接 MSG_ALL.db 数据库,并执行查询 - if len(databases) > 1: - db = sqlite3.connect(":memory:") - attach_databases(db, databases) - else: - db = sqlite3.connect(list(databases.values())[0]) + # # 连接 MSG_ALL.db 数据库,并执行查询 + # if len(databases) > 1: + # db = sqlite3.connect(":memory:") + # attach_databases(db, databases) + # else: + # db = sqlite3.connect(list(databases.values())[0]) outdb = sqlite3.connect(save_path) out_cursor = outdb.cursor() # 将MSG_db_paths中的数据合并到out_db_path中 for alias in databases: + db = sqlite3.connect(databases[alias]) # 获取表名 - sql = f"SELECT name FROM {alias}.sqlite_master WHERE type='table' ORDER BY name;" + sql = f"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name;" tables = execute_sql(db, sql) for table in tables: table = table[0] if table == "sqlite_sequence": continue - # 获取表中的数据 - sql = f"SELECT * FROM {alias}.{table}" - data = execute_sql(db, sql) - if len(data) < 1: - continue + # 获取表中的字段名 sql = f"PRAGMA table_info({table})" columns = execute_sql(db, sql) columns = [i[1] for i in columns] - if len(columns) < 1: + if not columns or len(columns) < 1: continue + # 检测表是否存在 sql = f"SELECT name FROM sqlite_master WHERE type='table' AND name='{table}'" out_cursor.execute(sql) @@ -252,15 +251,25 @@ def merge_db(db_paths, save_path="merge.db"): sql = f"CREATE UNIQUE INDEX IF NOT EXISTS {index_name} ON {table} ({coalesce_columns})" out_cursor.execute(sql) + # 获取表中的数据 + if "CreateTime" in columns and CreateTime > 0: + sql = f"SELECT * FROM {table} WHERE CreateTime>? ORDER BY CreateTime" + src_data = execute_sql(db, sql, (CreateTime,)) + else: + sql = f"SELECT * FROM {table}" + src_data = execute_sql(db, sql) + if not src_data or len(src_data) < 1: + continue # 插入数据 sql = f"INSERT OR IGNORE INTO {table} VALUES ({','.join(['?'] * len(columns))})" - out_cursor.executemany(sql, data) + out_cursor.executemany(sql, src_data) outdb.commit() outdb.close() # 断开数据库连接 - if len(databases) > 1: - for alias in databases: - db.execute(f"DETACH DATABASE {alias}") - db.close() + # if len(databases) > 1: + # for alias in databases: + # db.execute(f"DETACH DATABASE {alias}") + # db.close() return save_path +