diff --git a/config.py b/config.py index 50410a7..ef693fe 100644 --- a/config.py +++ b/config.py @@ -1,7 +1,7 @@ # TODO Database verbindung herstellen -# Database Verbinungsdaten +# Database Verbindungsdaten user = 'root' password = '' -host = 'localhost' \ No newline at end of file +host = 'localhost' diff --git a/database.py b/database.py index 3872e2e..6899a9b 100644 --- a/database.py +++ b/database.py @@ -5,17 +5,22 @@ import mysql.connector # Create Database or Table def create(db_cursor, name, type_): + # TODO Fix nötig query = f'CREATE {type_} {name};' - print(query) + if type_ == 'TABLE': + query = f'CREATE {type_} {name} (ID integer primary key auto_increment, question varchar(255), answers varchar(255));' try: db_cursor.execute(query) except mysql.connector.Error as err: print('Something went wrong', err) -def read(db_cursor, name, type_): - # TODO - return +def read(db_cursor, name): + query = f'SELECT * FROM {name}' + try: + db_cursor.execute(query) + except mysql.connector.Error as err: + print('Something went wrong', err) def update(db_cursor, name, type_): @@ -31,11 +36,12 @@ def del_(db_cursor, name, type_): print('Something went wrong', err) -def add(db_cursor, name, type_, question, answer): - query = f'INSERT INTO {type_} {name} (question, answer)' - val = f'values ({question},{answer});' - db_cursor.execute(query, val) - return +def add(db_cursor, name, question, answer): + query = f'INSERT INTO {name} (question, answer) VALUES ({question}, {answer});' + try: + db_cursor.execute(query) + except mysql.connector.Error as err: + print('Something went wrong', err) def show(db_cursor, type_): @@ -46,4 +52,17 @@ def show(db_cursor, type_): print('Something went wrong', err) +def use(db_cursor, name): + query = f'USE {name};' + try: + db_cursor.execute(query) + except mysql.connector.Error as err: + 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) \ No newline at end of file diff --git a/database_connection.py b/database_connection.py index 03937b7..9eb0246 100644 --- a/database_connection.py +++ b/database_connection.py @@ -6,12 +6,12 @@ import mysql.connector # Aufbau der verbindung zum Mysql Server -def db_connection(user, password, host): +def db_connection(user, password, host, database): connection = '' try: - # Verbindung mit die in der Config festgelegt werden - connection = mysql.connector.connect(user=user, password=password, host=host) + # Verbindung - Eingaben in der config.py bestimmen + connection = mysql.connector.connect(user=user, password=password, host=host, database=database) except mysql.connector.Error as err: print('Something went wrong', err) print('Veruche deinen Mysql Server neu zu starten') diff --git a/database_menu.py b/database_menu.py index 2f596a5..bf635df 100644 --- a/database_menu.py +++ b/database_menu.py @@ -6,17 +6,18 @@ import main_menu # Datenbank Menü def menu(): + connection = database_connection.db_connection(config.user, config.password, config.host, None) - selection = {0: 'Datenbanken anzeigen', 1: 'Datenbank erstellen', 2: 'Datenbank löschen', 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', 12: 'Zurück zum Hauptmenü'} + selection = {0: ' Datenbanken anzeigen', 1: ' Datenbank erstellen', 2: ' Datenbank löschen', 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', 12: 'Zurück zum Hauptmenü'} print('#Datenbank Menü# \n' '\nWas möchtest du tun?\n') while True: - connection = database_connection.db_connection(config.user, config.password, config.host) + cursor = connection.cursor() for key, value in selection.items(): print(key, value) @@ -26,8 +27,8 @@ def menu(): case 0: use_type = 'DATABASES' database.show(cursor, use_type) - print('# Dataenbanken #') - print(str(cursor.fetchall()).replace(',', '')) + print('# Datenbanken #') + print(str(cursor.fetchall()).replace(',', ' ')) case 1: use_type = 'DATABASE' print('# Datenbank erstellen #\n') @@ -42,8 +43,9 @@ def menu(): # TODO Funktion return case 4: - # TODO Funktion - return + name = input('Welche Datenbank möchtest du benutzen?\n') + database.use(cursor, name) + connection.database = name case 5: use_type = 'TABLE' print('# Tabelle erstellen #\n') @@ -56,13 +58,16 @@ def menu(): # TODO Funktion return case 8: - # TODO Funktion - return + print('# Tabelle Auslesen #\n') + name = input('Aus welcher Tabelle soll dir alles angezeigt werden?\n') + database.read(cursor, name) + case 9: # TODO Funktion return case 10: - # TODO Funktion + database.show_tables(cursor) + print(str(cursor.fetchall()).replace(',', '')) return case 11: table_name = str(input('Bitte den Tabellennamen angeben\n')) diff --git a/main_menu.py b/main_menu.py index e75127d..0833bd3 100644 --- a/main_menu.py +++ b/main_menu.py @@ -5,7 +5,7 @@ import database_menu def main_menu(): enable_menu = True - selection = {0: 'Datenbank Menü', 1: 'Zufallsfrage', 2: 'Programm beenden'} + selection = {0: 'Datenbank Menü', 1: 'Zufallsfrage', 2: 'Programm beenden\n'} print('# Main Menü #\n') while enable_menu: @@ -20,5 +20,4 @@ def main_menu(): # TODO Starte Zufallsfragenausgabe return case 2: - # TODO Programm Beenden - return + break