From edd501b8c59eaffddcdaced822b00cf0875dad1d Mon Sep 17 00:00:00 2001 From: xaoyaoo Date: Tue, 12 Dec 2023 15:50:12 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=90=88=E5=B9=B6=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E5=BA=93=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pywxdump/wx_info/merge_db.py | 39 +++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/pywxdump/wx_info/merge_db.py b/pywxdump/wx_info/merge_db.py index 839e969..3c4bbfc 100644 --- a/pywxdump/wx_info/merge_db.py +++ b/pywxdump/wx_info/merge_db.py @@ -200,6 +200,13 @@ def execute_sql(connection, sql, params=None): def merge_db(db_paths, save_path="merge.db", CreateTime: int = 0): + """ + 合并数据库 会忽略主键以及重复的行。 + :param db_paths: + :param save_path: + :param CreateTime: + :return: + """ if os.path.isdir(save_path): save_path = os.path.join(save_path, f"merge_{int(time.time())}.db") @@ -223,12 +230,13 @@ def merge_db(db_paths, save_path="merge.db", CreateTime: int = 0): table = table[0] if table == "sqlite_sequence": continue - # 获取表中的字段名 sql = f"PRAGMA table_info({table})" columns = execute_sql(db, sql) - # col_type = {(i[1], i[2]) for i in columns} - columns = [i[1] for i in columns] + col_type = { + (i[1] if isinstance(i[1], str) else i[1].decode(), i[2] if isinstance(i[2], str) else i[2].decode()) for + i in columns} + columns = [i[1] if isinstance(i[1], str) else i[1].decode() for i in columns] if not columns or len(columns) < 1: continue @@ -237,15 +245,15 @@ def merge_db(db_paths, save_path="merge.db", CreateTime: int = 0): out_cursor.execute(sql) if len(out_cursor.fetchall()) < 1: # 创建表 - # # 拼接创建表的SQL语句 - # column_definitions = [] - # for column in col_type: - # column_name = column[0] - # column_type = column[1] - # column_definition = f"{column_name} {column_type}" - # column_definitions.append(column_definition) - # sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(column_definitions)})" - sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(columns)})" + # 拼接创建表的SQL语句 + column_definitions = [] + for column in col_type: + column_name = column[0] if isinstance(column[0], str) else column[0].decode() + column_type = column[1] if isinstance(column[1], str) else column[1].decode() + column_definition = f"{column_name} {column_type}" + column_definitions.append(column_definition) + sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(column_definitions)})" + # sql = f"CREATE TABLE IF NOT EXISTS {table} ({','.join(columns)})" out_cursor.execute(sql) # 创建包含 NULL 值比较的 UNIQUE 索引 @@ -256,17 +264,16 @@ def merge_db(db_paths, save_path="merge.db", CreateTime: int = 0): # 获取表中的数据 if "CreateTime" in columns and CreateTime > 0: - sql = f"SELECT * FROM {table} WHERE CreateTime>? ORDER BY CreateTime" + sql = f"SELECT {','.join([i[0] for i in col_type])} FROM {table} WHERE CreateTime>? ORDER BY CreateTime" src_data = execute_sql(db, sql, (CreateTime,)) else: - sql = f"SELECT * FROM {table}" + sql = f"SELECT {','.join([i[0] for i in col_type])} 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))})" + sql = f"INSERT OR IGNORE INTO {table} ({','.join([i[0] for i in col_type])}) VALUES ({','.join(['?'] * len(columns))})" out_cursor.executemany(sql, src_data) outdb.commit() outdb.close() return save_path -