studia/jezyki-skryptowe/lista10/database.py
2024-06-14 16:53:58 +02:00

28 lines
636 B
Python

from peewee import SqliteDatabase
from models.rental import Rental
from models.station import Station
_db = None
def init_database(db_name):
global _db
if _db is None:
_db = SqliteDatabase(db_name)
Station._meta.database = _db
Rental._meta.database = _db
return _db
def get_database():
global _db
if _db is None:
raise ValueError("Database is not initialized. Call init_database() first.")
return _db
def close_database():
global _db
if _db is not None:
_db.close()
Station._meta.database = None
Rental._meta.database = None
_db = None