divatool/db/base.py
2021-12-28 15:49:14 -05:00

36 lines
944 B
Python

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])
)