rebase
This commit is contained in:
parent
b5de16c01b
commit
7a56e75a5c
11
README.md
Normal file
11
README.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
<h1> Projekt Fragen - Katalog</h1>
|
||||
|
||||
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
|
||||
<br>
|
||||
<br>
|
||||
|
||||
<h4>Um das Programm zu starten:</h4>
|
||||
Führe die 'start.bat' aus.
|
23
app.py
Normal file
23
app.py
Normal file
|
@ -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()
|
0
assets/__init__.py
Normal file
0
assets/__init__.py
Normal file
8
assets/config.py
Normal file
8
assets/config.py
Normal file
|
@ -0,0 +1,8 @@
|
|||
# TODO Database verbindung herstellen
|
||||
|
||||
# Database Verbindungsdaten
|
||||
|
||||
user = 'root'
|
||||
password = ''
|
||||
host = 'localhost'
|
||||
json_file = 'assets/questions_answers.json'
|
24
assets/question_answer_output.py
Normal file
24
assets/question_answer_output.py
Normal file
|
@ -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])
|
||||
|
9
assets/questions_answers.json
Normal file
9
assets/questions_answers.json
Normal file
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
||||
]
|
8
assets/requirements_installer.py
Normal file
8
assets/requirements_installer.py
Normal file
|
@ -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'])
|
0
database/__init__.py
Normal file
0
database/__init__.py
Normal file
87
database/database.py
Normal file
87
database/database.py
Normal file
|
@ -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)
|
15
database/database_connection.py
Normal file
15
database/database_connection.py
Normal file
|
@ -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
|
0
menu/__init__.py
Normal file
0
menu/__init__.py
Normal file
83
menu/database_menu.py
Normal file
83
menu/database_menu.py
Normal file
|
@ -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()
|
30
menu/main_menu.py
Normal file
30
menu/main_menu.py
Normal file
|
@ -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
|
10
start.bat
Normal file
10
start.bat
Normal file
|
@ -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
|
5
start_app.py
Normal file
5
start_app.py
Normal file
|
@ -0,0 +1,5 @@
|
|||
import os
|
||||
|
||||
|
||||
# Startet das Programm in der Windows Konsole
|
||||
os.system("start /wait cmd /c py app.py")
|
Loading…
Reference in New Issue
Block a user