39 lines
701 B
Python
39 lines
701 B
Python
import atexit
|
|
import os
|
|
import sqlite3
|
|
|
|
|
|
_schema_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "schema.sql")
|
|
_db = None
|
|
_path = "items.sqlite3"
|
|
|
|
|
|
def set_db_path(f: str) -> None:
|
|
global _path
|
|
_path = f
|
|
|
|
|
|
def get_db() -> sqlite3.Connection:
|
|
global _db
|
|
if not _db:
|
|
_db = sqlite3.connect(
|
|
_path,
|
|
detect_types=sqlite3.PARSE_DECLTYPES,
|
|
)
|
|
_db.row_factory = sqlite3.Row
|
|
return _db
|
|
|
|
|
|
@atexit.register
|
|
def close_db() -> None:
|
|
global _db
|
|
if _db:
|
|
_db.close()
|
|
_db = None
|
|
|
|
|
|
def create_db() -> None:
|
|
db = get_db()
|
|
with open(_schema_path, encoding="utf-8") as f:
|
|
db.executescript(f.read())
|