import os import sqlite3 DATABASES = { "QuestMst.db": "Quest", "QuestSceneMst.db": "QuestScene", "QuestPartMst.db": "QuestPart", "ResourceEntry.db": "ResourceEntry", } class BaseDB(object): conn = None cursor = None def __init__(self, path, databases): self.conn = sqlite3.connect(":memory:") self.conn.row_factory = sqlite3.Row self.cursor = self.conn.cursor() self.attach_dbs(databases, path) def __enter__(self): return self def __exit__(self, exc_type, exc_value, traceback): self.conn.close() def attach_dbs(self, databases, path): for d in databases: if not os.path.isfile(os.path.join(path, d)): raise FileNotFoundError(f"Database {d} not found") for d in databases.items(): self.cursor.execute( "ATTACH DATABASE ? AS ?", (os.path.join(path, d[0]), d[1]) )