Files
s2p_banking_system/database.py
2024-05-21 00:05:43 +02:00

34 lines
1.1 KiB
Python

# Lucas Mathews - Fontys Student ID: 5023572
# Banking System Manager File
import os.path
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# Import Config
from config import CONFIG
# Check if the database exists
if os.path.exists(CONFIG["database"]["name"]):
print(f"Database {CONFIG["database"]["name"]} already exists.")
else:
print(f"Database {CONFIG["database"]["name"]} does not exist. Creating it now.")
# Sets the database file to be used from the configuration file
db_url : str = "sqlite:///" + CONFIG["database"]["name"]
print(f"Database file set to: {db_url}")
# Creates the database engine (does not create the database file if it already exists)
engine = create_engine(db_url, echo=True)
# Import base class
from class_base import Base # Imports the base class required by SQLAlchemy
# Create the tables in the database
Base.metadata.create_all(bind=engine) # Creates the tables in the database from the classes
# Creates a session to interact with the database
Session = sessionmaker(bind=engine) # Creates a session to interact with the database
session = Session() # Creates a session object