From 4ed70bb1d5b00ff879eb0504d551462fa147a201 Mon Sep 17 00:00:00 2001 From: marcus Date: Thu, 24 Mar 2022 10:41:17 +0100 Subject: [PATCH] 24.03.2022 Database & Menu update --- database/database.py | 40 +++++++++++++++++----------------------- menu/database_menu.py | 25 ++++++++++++++----------- 2 files changed, 31 insertions(+), 34 deletions(-) diff --git a/database/database.py b/database/database.py index 3eacba0..49fa962 100644 --- a/database/database.py +++ b/database/database.py @@ -1,7 +1,7 @@ # TODO: Datenbank Klasse (CRUD) # # TODO: Tabellen und Spaltennamen selbst bestimmen - +import re import mysql.connector @@ -11,13 +11,14 @@ def create(db_cursor, name, type_): # TODO Fix nötig query = f'CREATE {type_} {name};' if type_ == 'TABLE': - query = f'CREATE {type_} {name} (ID integer primary key auto_increment, question varchar(255), answers varchar(255));' + query = f'CREATE {type_} {name}' try: db_cursor.execute(query) except mysql.connector.Error as err: print('Something went wrong', err) +# Datenbank auslesen def read(db_cursor, name): query = f'SELECT * FROM {name}' try: @@ -26,11 +27,12 @@ def read(db_cursor, name): print('Something went wrong', err) +# TODO def update(db_cursor, name, type_): - # TODO return +# Löschen von Datenbank oder Tabelle def del_(db_cursor, name, type_): query = f'DROP {type_} IF EXISTS {name};' try: @@ -38,23 +40,26 @@ def del_(db_cursor, name, type_): except mysql.connector.Error as err: print('Something went wrong', err) -# TODO hier die Spaltennamen selbst bestimmen -def add(db_cursor, name, question, answer): - query = f'INSERT INTO {name} (question, answer) VALUES ({question}, {answer});' + +# Spalten anlegen +def add_columns(db_cursor, column_name, table_name): + query = f'INSERT INTO {table_name} {column_name};' try: db_cursor.execute(query) except mysql.connector.Error as err: print('Something went wrong', err) -def show(db_cursor, type_): - query = f'SHOW {type_};' +# TODO +def add_values(db_cursor, table_name, column_name, value): + query = '' try: db_cursor.execute(query) except mysql.connector.Error as err: print('Something went wrong', err) +# Datenbank benutzen def use(db_cursor, name): query = f'USE {name};' try: @@ -63,25 +68,14 @@ def use(db_cursor, name): print('Something went wrong', err) -def show_tables(db_cursor): - query = 'show tables;' - try: - db_cursor.execute(query) - except mysql.connector.Error as err: - print('Something went wrong', err) - - -def show(db_cursor, use_type): +# Zeigt Datenbanken oder Tabellen in der ausgewähltem Datenbank anzeigen +def show_all(db_cursor, use_type): query = f'show {use_type}' try: db_cursor.execute(query) for i in db_cursor.fetchall(): - print(str(i).replace(',','').replace('(', '').replace(')', '')) + string_ = re.sub('[,()]', '', str(i)) + print(string_) print('\n') except mysql.connector.Error as err: print('Something went wrong', err) - - -def string_formater(_string): - _string.replace(',','').replace('(', '').replace(')', '') - return str(_string) \ No newline at end of file diff --git a/menu/database_menu.py b/menu/database_menu.py index 4fa1490..0fdcdde 100644 --- a/menu/database_menu.py +++ b/menu/database_menu.py @@ -12,7 +12,7 @@ def menu(): 3: ' Datenbank umbenennen', 4: ' Datenbanken benutzen', 5: ' Tabelle erstellen', 6: ' Tabelle umbenennen', 7: ' Tabelle löschen', 8: ' Tabelle auslesen', - 9: ' In Tabelle einfügen', 10: 'Tabellen anzeigen', 11: 'Frage/Antwort hinzufügen', + 9: ' Spalten erstellen', 10: 'Tabellen anzeigen', 11: 'Frage/Antwort hinzufügen', 12: 'Zurück zum Hauptmenü'} while connection.is_connected(): @@ -28,7 +28,7 @@ def menu(): case 0: use_type = 'DATABASES' print('# Datenbanken #\n') - database.show(cursor, use_type) + database.show_all(cursor, use_type) case 1: use_type = 'DATABASE' print('# Datenbank erstellen #\n') @@ -36,7 +36,7 @@ def menu(): database.create(cursor, database_name, use_type) case 2: use_type = 'DATABASE' - print('# Datenbank löschen #\n') # Platzhalter für die Funktion + print('# Datenbank löschen #\n') database_name = input('Welche Datenbank soll gelöscht werden?\n') database.del_(cursor, database_name, use_type) case 3: @@ -61,19 +61,22 @@ def menu(): print('# Tabelle Auslesen #\n') name = input('Aus welcher Tabelle soll dir alles angezeigt werden?\n') database.read(cursor, name) - case 9: - print('--Funktion ist in Arbeit-- \n') - continue + print('# Spalten erstellen #\n') + table_name = input('In welcher Tabelle möchtest du spalten anlegen? Achtung, Tabelle muss vorhanden ' + 'sein! \n') + column_name = input('Wie soll die Spalte heißen?\n') + database.add_columns(cursor,table_name,column_name) case 10: - database.show_tables(cursor) - print(str(cursor.fetchall()).replace(',', '')) - return + print(' # Tabellen anzeigen #\n ') + use_type = 'TABLES' + database.show_all(cursor, use_type) case 11: - table_name = str(input('Bitte den Tabellennamen angeben\n')) + # TODO überarbneitung nötig! + """table_name = str(input('Bitte den Tabellennamen angeben\n')) print("#Fragen und Antworten hinzufügen #\n") question = str(input('Bitte die Frage eingeben\n')) answer = str(input('Bitte die dazugehörige antwort eingeben\n')) - database.add(cursor, table_name, question, answer) + database.add(cursor, table_name, question, answer)""" case 12: main_menu.main_menu()