From 0744cc8606a26324bf5940379f50e7be74f48b25 Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 2 Dec 2022 12:58:18 +0100 Subject: [PATCH] started new database implementation, the old one was way to bad --- src/music_kraken/database/new_database.py | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 src/music_kraken/database/new_database.py diff --git a/src/music_kraken/database/new_database.py b/src/music_kraken/database/new_database.py new file mode 100644 index 0000000..468c961 --- /dev/null +++ b/src/music_kraken/database/new_database.py @@ -0,0 +1,37 @@ +import sqlite3 +import os +import logging + +logger = logging.getLogger("database") + + +class Database: + def __init__(self, structure_file: str, database_file: str): + self.structure_file: str = structure_file + self.database_file: str = database_file + + self.connection = sqlite3.connect(self.database_file) + self.cursor = self.connection.cursor() + + def reset(self): + """ + Deletes all Data from the database if it exists + and resets the schema defined in self.structure_file + """ + logger.info(f"resetting the database \"{self.__name__}\"") + + # deleting the database + del self.connection + del self.cursor + os.remove(self.database_file) + + # newly creating the database + self.connection = sqlite3.connect(self.database_file) + self.cursor = self.connection.cursor() + with open(self.structure_file, "r") as structure_file_obj: + query = structure_file_obj.read() + + # fill the database with the schematic + self.cursor.executescript(query) + self.connection.commit() + \ No newline at end of file