From cf6abeaeb0f3cbcfdefb0353259c7630eef3cffd Mon Sep 17 00:00:00 2001 From: marcus Date: Fri, 18 Mar 2022 12:30:09 +0100 Subject: [PATCH] #5 --- config.py | 2 +- database.py | 5 +++-- database_connection.py | 2 ++ database_menu.py | 46 ++++++++++++++++++++++++++++++++++++++++++ main.py | 6 ++++-- main_menu.py | 24 ++++++++++++++++++++++ selection_menu.py | 28 ------------------------- 7 files changed, 80 insertions(+), 33 deletions(-) create mode 100644 database_menu.py create mode 100644 main_menu.py delete mode 100644 selection_menu.py diff --git a/config.py b/config.py index 6629ebc..50410a7 100644 --- a/config.py +++ b/config.py @@ -1,6 +1,6 @@ # TODO Database verbindung herstellen -# Database +# Database Verbinungsdaten user = 'root' password = '' diff --git a/database.py b/database.py index 15c7d3c..3d9e9b3 100644 --- a/database.py +++ b/database.py @@ -2,8 +2,9 @@ import mysql.connector -def create(db_cursor, name): - query = f'CREATE DATABASE {name}' +# Create Database or Table +def create(db_cursor, name, _type): + query = f'CREATE {_type} {name}' try: db_cursor.execute(query) except mysql.connector.Error as err: diff --git a/database_connection.py b/database_connection.py index ad738b3..aee5fb8 100644 --- a/database_connection.py +++ b/database_connection.py @@ -3,10 +3,12 @@ import mysql.connector +# Aufbau der verbindung zum Mysql Server def db_connection(user, password, host): connection = '' try: + # Verbindung mit die in der Config festgelegt werden connection = mysql.connector.connect(user=user, password=password, host=host) except mysql.connector.Error as err: print('Something went wrong', err) diff --git a/database_menu.py b/database_menu.py new file mode 100644 index 0000000..f18a45b --- /dev/null +++ b/database_menu.py @@ -0,0 +1,46 @@ +import database +import database_connection +import config + +# Auskommentieren falls Server offline +cursor = database_connection.db_connection(config.user, config.password, config.host) + + +# Datenbank Menü +def menu(): + enable_menu = True + selection = {0: 'Datenbanken anzeigen', 1: 'Datenbank erstellen', 2: 'Datenbank löschen', 3: 'Datenbank umbenennen', + 4: 'Datenbank auselesen', + 5: 'Tabelle erstellen', 6: 'Tabelle umbenennen', 7: 'Tabelle löschen', 8: 'Tabelle auselsen', + 9: 'In Tabelle einfügen', 10: 'Tabellen anzeigen'} + + print('#Datenbank Menü# \n' + '\nWas möchtest du tun?\n') + + while enable_menu: + for key, value in selection.items(): + print(key, value) + selection_number = int(input('Bitte Auswahl treffen')) + + match selection_number: + case 1: + _type = 'DATABASE' + print('#Datenbank erstellen#') + database_name = input('Wie soll die Datenbank heißen?') + database.create(cursor, database_name, _type) + case 2: + # TODO Funktion + print('Datenbank löschen') # Platzhalter für die Funktion + case 3: + # TODO Funktion + return + case 4: + # TODO Funktion + return + case 5: + _type = 'TABLE' + print('#Tabelle erstellen#') + table_name = input('Wie soll die Datenbank heißen?') + database.create(cursor, table_name, _type) + + break diff --git a/main.py b/main.py index 184c111..c0cbce8 100644 --- a/main.py +++ b/main.py @@ -3,11 +3,13 @@ # Ein und Ausgabe der Fragen/Antworten # Datenbanken (CRUD) Tabellen (CRUD) -import selection_menu +import main_menu +# Aufruf des Main Menüse def main(): - selection_menu.menu() + main_menu.main_menu() +# Starten der Main main() diff --git a/main_menu.py b/main_menu.py new file mode 100644 index 0000000..e75127d --- /dev/null +++ b/main_menu.py @@ -0,0 +1,24 @@ +import database_menu + + +# Main Menu + +def main_menu(): + enable_menu = True + selection = {0: 'Datenbank Menü', 1: 'Zufallsfrage', 2: 'Programm beenden'} + + print('# Main Menü #\n') + while enable_menu: + for key, value in selection.items(): + print(key, value) + selection_number = int(input('Bitte Auswahl treffen\n')) + match selection_number: + case 0: + database_menu.menu() + break + case 1: + # TODO Starte Zufallsfragenausgabe + return + case 2: + # TODO Programm Beenden + return diff --git a/selection_menu.py b/selection_menu.py deleted file mode 100644 index 683334d..0000000 --- a/selection_menu.py +++ /dev/null @@ -1,28 +0,0 @@ -import database -import database_connection -import config - -cursor = database_connection.db_connection(config.user, config.password, config.host) - - -def menu(): - enable_menu = True - selection = {1: 'Datenbank erstellen', 2: 'Datenbank löschen', 3: 'Datenbank umbenennen', 4: 'Datenbank auselesen', - 5: 'Tabelle erstellen'} - - print('Was möchtest du tun?') - - while enable_menu: - for key, value in selection.items(): - print(key, value) - selection_number = int(input('Bitte Auswahl treffen')) - - match selection_number: - case 1: - print('#Datenbank erstellen#') # Platzhalter für die Funktion - database_name = input('Wie soll die Datenbank heißen?') - database.create(cursor, database_name) - case 2: - # TODO Funktion - print('Datenbank löschen') # Platzhalter für die Funktion - break