add database and schemas

This commit is contained in:
WorldTeacher
2024-07-05 10:05:29 +02:00
parent 8811fe0a98
commit 8c45b07642
2 changed files with 63 additions and 0 deletions

38
src/logic/database.py Normal file
View File

@@ -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

25
src/logic/db_schemas.py Normal file
View File

@@ -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)
"""