From 7a56e75a5c53c1c44b9e02f0dbc501271de42e9a Mon Sep 17 00:00:00 2001 From: marcusferl Date: Tue, 22 Mar 2022 23:09:39 +0100 Subject: [PATCH] rebase --- README.md | 11 ++++ app.py | 23 +++++++++ assets/__init__.py | 0 assets/config.py | 8 +++ assets/question_answer_output.py | 24 +++++++++ assets/questions_answers.json | 9 ++++ assets/requirements_installer.py | 8 +++ database/__init__.py | 0 database/database.py | 87 ++++++++++++++++++++++++++++++++ database/database_connection.py | 15 ++++++ menu/__init__.py | 0 menu/database_menu.py | 83 ++++++++++++++++++++++++++++++ menu/main_menu.py | 30 +++++++++++ start.bat | 10 ++++ start_app.py | 5 ++ 15 files changed, 313 insertions(+) create mode 100644 README.md create mode 100644 app.py create mode 100644 assets/__init__.py create mode 100644 assets/config.py create mode 100644 assets/question_answer_output.py create mode 100644 assets/questions_answers.json create mode 100644 assets/requirements_installer.py create mode 100644 database/__init__.py create mode 100644 database/database.py create mode 100644 database/database_connection.py create mode 100644 menu/__init__.py create mode 100644 menu/database_menu.py create mode 100644 menu/main_menu.py create mode 100644 start.bat create mode 100644 start_app.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..2cd373c --- /dev/null +++ b/README.md @@ -0,0 +1,11 @@ +

Projekt Fragen - Katalog

+ +Ziel ist es ein Programm mit einer Datenbank zu erstellen, welches: +- Fragen und Antworen einließt +- Ein Auswahlmenü bietet +- Fragen abfragt und die Antwort ausgibt +
+
+ +

Um das Programm zu starten:

+Führe die 'start.bat' aus. \ No newline at end of file diff --git a/app.py b/app.py new file mode 100644 index 0000000..d30c831 --- /dev/null +++ b/app.py @@ -0,0 +1,23 @@ +# TODO: Datenbankabfrage +# Auswahlmenü (Neue Fragen/ Oder Abfrage) +# Ein und Ausgabe der Fragen/Antworten +# Datenbanken (CRUD) Tabellen (CRUD) + +from menu import main_menu +from assets import requirements_installer + + +# Aufruf des Main Menüs +def main(): + print('Programm wird in 5 Sekunden gestartet, bitte gewährleiste, dass beim Programmstart dein Mysql Server ' + 'online ist\n') + print('Installation vom Mysql Connector\n') + # Installiert die nötigen Ressourcen + requirements_installer.installer() + + print('\n ----IHK Prüfungsfragen---- \n') + main_menu.main_menu() + + +# Starten der Main +main() diff --git a/assets/__init__.py b/assets/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/assets/config.py b/assets/config.py new file mode 100644 index 0000000..e481aff --- /dev/null +++ b/assets/config.py @@ -0,0 +1,8 @@ +# TODO Database verbindung herstellen + +# Database Verbindungsdaten + +user = 'root' +password = '' +host = 'localhost' +json_file = 'assets/questions_answers.json' \ No newline at end of file diff --git a/assets/question_answer_output.py b/assets/question_answer_output.py new file mode 100644 index 0000000..4c861af --- /dev/null +++ b/assets/question_answer_output.py @@ -0,0 +1,24 @@ +import json +from random import randrange + + +# Einlesen vom Json File +def read_json(filename): + with open(filename,encoding='utf-8') as filename: + data = json.load(filename) + return data + + +# Zufällige Frage erstellen +def random_question(filename): + min_ = 0 + max_ = len(read_json(filename)) + random_number = randrange(min_,max_) + questions = read_json(filename) + question = f'Frage #{random_number}: ' + questions[random_number][str(random_number)]['question'] + answer = questions[random_number][str(random_number)]['answer'] + return question, answer + + +#print(random_question(config.json_file)[0]) + diff --git a/assets/questions_answers.json b/assets/questions_answers.json new file mode 100644 index 0000000..46ab580 --- /dev/null +++ b/assets/questions_answers.json @@ -0,0 +1,9 @@ +[ + { + "0": { + "question": "Was versteht man unter dem Begriff Cross-Selling?", + "answer": "Eine Verkaufsstrategie, bei der zu einem Produkt, zusätzliche ( inhalte, produkte, services etc...) angeboten werden, um einen höheren Umsatz und die Kundenanbindung zu erhöhen" + } + } + +] \ No newline at end of file diff --git a/assets/requirements_installer.py b/assets/requirements_installer.py new file mode 100644 index 0000000..57d1471 --- /dev/null +++ b/assets/requirements_installer.py @@ -0,0 +1,8 @@ +import sys +import subprocess + + +# Ressourcen für den Autoinstaller +def installer(): + subprocess.check_call([sys.executable, '-m', 'pip', 'install', + 'mysql-connector-python']) diff --git a/database/__init__.py b/database/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/database/database.py b/database/database.py new file mode 100644 index 0000000..3eacba0 --- /dev/null +++ b/database/database.py @@ -0,0 +1,87 @@ +# TODO: Datenbank Klasse (CRUD) +# +# TODO: Tabellen und Spaltennamen selbst bestimmen + + +import mysql.connector + + +# Create Database or Table +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));' + try: + db_cursor.execute(query) + except mysql.connector.Error as err: + print('Something went wrong', err) + + +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_): + # TODO + return + + +def del_(db_cursor, name, type_): + query = f'DROP {type_} IF EXISTS {name};' + try: + db_cursor.execute(query) + 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});' + 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_};' + try: + db_cursor.execute(query) + except mysql.connector.Error as err: + 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) + + +def show(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(')', '')) + 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/database/database_connection.py b/database/database_connection.py new file mode 100644 index 0000000..d361475 --- /dev/null +++ b/database/database_connection.py @@ -0,0 +1,15 @@ +import mysql.connector + + +# Aufbau der verbindung zum Mysql Server + + +def db_connection(user, password, host, database): + connection = '' + try: + # 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') + return connection diff --git a/menu/__init__.py b/menu/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/menu/database_menu.py b/menu/database_menu.py new file mode 100644 index 0000000..dc23b77 --- /dev/null +++ b/menu/database_menu.py @@ -0,0 +1,83 @@ +from assets import config +from database import database +from database import database_connection +from menu 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ü'} + + + + while connection.is_connected(): + print('#Datenbank Menü# \n' + '\nWas möchtest du tun?\n') + cursor = connection.cursor() + + for key, value in selection.items(): + print(key, value) + selection_number = int(input('\nBitte Auswahl treffen\n')) + + match selection_number: + case 0: + use_type = 'DATABASES' + print('# Datenbanken #\n') + database.show(cursor, use_type) + case 1: + use_type = 'DATABASE' + print('# Datenbank erstellen #\n') + database_name = input('Wie soll die Datenbank heißen?\n') + database.create(cursor, database_name, use_type) + case 2: + use_type = 'DATABASE' + print('# Datenbank löschen #\n') # Platzhalter für die Funktion + database_name = input('Welche Datenbank soll gelöscht werden?\n') + database.del_(cursor, database_name, use_type) + case 3: + print('--Funktion ist in Arbeit-- \n') + continue + case 4: + 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') + table_name = input('Wie soll die Datenbank heißen?\n') + database.create(cursor, table_name, use_type) + case 6: + print('--Funktion ist in Arbeit-- \n') + continue + case 7: + print('--Funktion ist in Arbeit-- \n') + continue + case 8: + 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 + case 10: + database.show_tables(cursor) + print(str(cursor.fetchall()).replace(',', '')) + return + case 11: + 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) + case 12: + main_menu.main_menu() diff --git a/menu/main_menu.py b/menu/main_menu.py new file mode 100644 index 0000000..6de0c7e --- /dev/null +++ b/menu/main_menu.py @@ -0,0 +1,30 @@ +from assets import config, question_answer_output +from menu import database_menu + + +# Main Menu + +def main_menu(): + enable_menu = True + selection = {0: 'Datenbank Menü', 1: 'Zufallsfrage', 2: 'Programm beenden\n'} + + 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: + while True: + print('#Frage#\n' + '-' * 60 + '\n' + question_answer_output.random_question(config.json_file)[0]) + + input('\nBitte Enter drücken um die Antwort zu sehen\n') + print('#Antwort#\n' + question_answer_output.random_question(config.json_file)[1]) + number = int(input('\nZurück zum Hauptmenü\n 1: Ja \n 2: Nein, nächste Frage\n')) + if number == 1: + break + case 2: + break diff --git a/start.bat b/start.bat new file mode 100644 index 0000000..960ffb3 --- /dev/null +++ b/start.bat @@ -0,0 +1,10 @@ +@echo off +chcp 65001 +echo ##################################### +echo # Autor : Marcus Ferl # +echo # Beschreibung : IHK Fragenkatalog # +echo # Programm gestartet # +echo ##################################### +@py.exe app.py + +pause \ No newline at end of file diff --git a/start_app.py b/start_app.py new file mode 100644 index 0000000..e64d5fe --- /dev/null +++ b/start_app.py @@ -0,0 +1,5 @@ +import os + + +# Startet das Programm in der Windows Konsole +os.system("start /wait cmd /c py app.py")