import sqlite3 import os def merge_sqlite(sqlite_list, merged_db, table_name): if len(sqlite_list) == 0: return # merge sqlite if os.path.exists(merged_db): os.remove(merged_db) conn = sqlite3.connect(merged_db) cursor = conn.cursor() cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table_name,)) fetchone=cursor.fetchone() for sqlite in sqlite_list: if not fetchone: print(f"Table {table_name} does not exist in {merged_db}, creating...") # Get table schema from first db first_db = sqlite3.connect(sqlite) first_cursor = first_db.cursor() first_cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name=?", (table_name,)) first_table = first_cursor.fetchone() if first_table: create_table_sql = first_table[0] first_db.close() # Create table in merged db print(create_table_sql) cursor.execute(create_table_sql) # Merge sqlite into merged_db cursor.execute("ATTACH DATABASE '{}' AS merge_db".format(merged_db)) fetchone=True if fetchone: cursor.execute("ATTACH DATABASE '{}' AS db".format(sqlite)) cursor.execute("INSERT OR IGNORE INTO merge_db.{} SELECT * FROM db.{}".format(table_name, table_name)) cursor.execute("COMMIT") cursor.execute("DETACH DATABASE db") cursor.execute("DETACH DATABASE merge_db") conn.commit() conn.close() if __name__ == '__main__': merge_sqlite(['./cache/temp.sqlite', './local/slow_query/1372813089209061633/1372813089454525346/1379661944646413143/slowquery/sqlite/2023122403/1703386800_1703390400_slowlog.sqlite'], './cache/merge.sqlite', 'slowlog')