From 8c45b07642a0305b977ed6961daa6c06f64c5ffd Mon Sep 17 00:00:00 2001 From: WorldTeacher <41587052+WorldTeacher@users.noreply.github.com> Date: Fri, 5 Jul 2024 10:05:29 +0200 Subject: [PATCH] add database and schemas --- src/logic/database.py | 38 ++++++++++++++++++++++++++++++++++++++ src/logic/db_schemas.py | 25 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 src/logic/database.py create mode 100644 src/logic/db_schemas.py diff --git a/src/logic/database.py b/src/logic/database.py new file mode 100644 index 0000000..1bdc54c --- /dev/null +++ b/src/logic/database.py @@ -0,0 +1,38 @@ +import sqlite3 +import os +from src import config +from pathlib import Path +from src.logic import USERS, MEDIA, LOANS +class Database: + def __init__(self) -> None: + self.db_name = config.database.name + self.db_location = config.database.path + self.db_path = Path(self.db_location).joinpath(self.db_name) + print(self.db_path) + self.backup_path = config.database.backupLocation + try: + self.connection = sqlite3.connect(self.db_path) + except sqlite3.OperationalError as e: + print(e) + self.createDatabase() + pass + + def createDatabase(self): + print("Creating Database") + if not os.path.exists(self.db_location): + os.makedirs(self.db_location) + self.connection = sqlite3.connect(self.db_path) + self.cursor = self.connection.cursor() + self.cursor.execute(USERS) + self.cursor.execute(MEDIA) + self.cursor.execute(LOANS) + self.connection.commit() + def tableCheck(self): + pass + + def test(self): + print("Test") + def checkUserExists(self, **kwargs): + pass + + \ No newline at end of file diff --git a/src/logic/db_schemas.py b/src/logic/db_schemas.py new file mode 100644 index 0000000..ac5a5ac --- /dev/null +++ b/src/logic/db_schemas.py @@ -0,0 +1,25 @@ +USERS = """ +CREATE TABLE IF NOT EXISTS users ( + id INTEGER PRIMARY KEY, + username TEXT NOT NULL, + usermain TEXT NOT NULL +""" #id == matrikelnr, +MEDIA = """ +CREATE TABLE IF NOT EXISTS media ( + id AUTOINCREMENT PRIMARY KEY, + signature TEXT NOT NULL, + isbn TEXT NOT NULL, + ppn TEXT NOT NULL, + title TEXT NOT NULL, + """ + +LOANS = """ +CREATE TABLE IF NOT EXISTS loans ( + id AUTOINCREMENT PRIMARY KEY, + user_id INTEGER NOT NULL, + media_id INTEGER NOT NULL, + loan_date TEXT NOT NULL, + return_date TEXT NOT NULL, + FOREIGN KEY (user_id) REFERENCES users(id), + FOREIGN KEY (media_id) REFERENCES media(id) +""" \ No newline at end of file