19.3.2022

This commit is contained in:
marcusferl 2022-03-19 20:55:12 +01:00
parent 356b1a7f7c
commit 25be3b8739
5 changed files with 52 additions and 29 deletions

View File

@ -1,6 +1,6 @@
# TODO Database verbindung herstellen # TODO Database verbindung herstellen
# Database Verbinungsdaten # Database Verbindungsdaten
user = 'root' user = 'root'
password = '' password = ''

View File

@ -5,17 +5,22 @@ import mysql.connector
# Create Database or Table # Create Database or Table
def create(db_cursor, name, type_): def create(db_cursor, name, type_):
# TODO Fix nötig
query = f'CREATE {type_} {name};' 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: try:
db_cursor.execute(query) db_cursor.execute(query)
except mysql.connector.Error as err: except mysql.connector.Error as err:
print('Something went wrong', err) print('Something went wrong', err)
def read(db_cursor, name, type_): def read(db_cursor, name):
# TODO query = f'SELECT * FROM {name}'
return try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)
def update(db_cursor, name, type_): def update(db_cursor, name, type_):
@ -31,11 +36,12 @@ def del_(db_cursor, name, type_):
print('Something went wrong', err) print('Something went wrong', err)
def add(db_cursor, name, type_, question, answer): def add(db_cursor, name, question, answer):
query = f'INSERT INTO {type_} {name} (question, answer)' query = f'INSERT INTO {name} (question, answer) VALUES ({question}, {answer});'
val = f'values ({question},{answer});' try:
db_cursor.execute(query, val) db_cursor.execute(query)
return except mysql.connector.Error as err:
print('Something went wrong', err)
def show(db_cursor, type_): def show(db_cursor, type_):
@ -46,4 +52,17 @@ def show(db_cursor, type_):
print('Something went wrong', 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)

View File

@ -6,12 +6,12 @@ import mysql.connector
# Aufbau der verbindung zum Mysql Server # Aufbau der verbindung zum Mysql Server
def db_connection(user, password, host): def db_connection(user, password, host, database):
connection = '' connection = ''
try: try:
# Verbindung mit die in der Config festgelegt werden # Verbindung - Eingaben in der config.py bestimmen
connection = mysql.connector.connect(user=user, password=password, host=host) connection = mysql.connector.connect(user=user, password=password, host=host, database=database)
except mysql.connector.Error as err: except mysql.connector.Error as err:
print('Something went wrong', err) print('Something went wrong', err)
print('Veruche deinen Mysql Server neu zu starten') print('Veruche deinen Mysql Server neu zu starten')

View File

@ -6,17 +6,18 @@ import main_menu
# Datenbank Menü # Datenbank Menü
def menu(): 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', selection = {0: ' Datenbanken anzeigen', 1: ' Datenbank erstellen', 2: ' Datenbank löschen', 3: ' Datenbank umbenennen',
4: 'Datenbanken benutzen', 4: ' Datenbanken benutzen',
5: 'Tabelle erstellen', 6: 'Tabelle umbenennen', 7: 'Tabelle löschen', 8: 'Tabelle auslesen', 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ü'} 9: ' In Tabelle einfügen', 10: 'Tabellen anzeigen', 11: 'Frage/Antwort hinzufügen', 12: 'Zurück zum Hauptmenü'}
print('#Datenbank Menü# \n' print('#Datenbank Menü# \n'
'\nWas möchtest du tun?\n') '\nWas möchtest du tun?\n')
while True: while True:
connection = database_connection.db_connection(config.user, config.password, config.host)
cursor = connection.cursor() cursor = connection.cursor()
for key, value in selection.items(): for key, value in selection.items():
print(key, value) print(key, value)
@ -26,8 +27,8 @@ def menu():
case 0: case 0:
use_type = 'DATABASES' use_type = 'DATABASES'
database.show(cursor, use_type) database.show(cursor, use_type)
print('# Dataenbanken #') print('# Datenbanken #')
print(str(cursor.fetchall()).replace(',', '')) print(str(cursor.fetchall()).replace(',', ' '))
case 1: case 1:
use_type = 'DATABASE' use_type = 'DATABASE'
print('# Datenbank erstellen #\n') print('# Datenbank erstellen #\n')
@ -42,8 +43,9 @@ def menu():
# TODO Funktion # TODO Funktion
return return
case 4: case 4:
# TODO Funktion name = input('Welche Datenbank möchtest du benutzen?\n')
return database.use(cursor, name)
connection.database = name
case 5: case 5:
use_type = 'TABLE' use_type = 'TABLE'
print('# Tabelle erstellen #\n') print('# Tabelle erstellen #\n')
@ -56,13 +58,16 @@ def menu():
# TODO Funktion # TODO Funktion
return return
case 8: case 8:
# TODO Funktion print('# Tabelle Auslesen #\n')
return name = input('Aus welcher Tabelle soll dir alles angezeigt werden?\n')
database.read(cursor, name)
case 9: case 9:
# TODO Funktion # TODO Funktion
return return
case 10: case 10:
# TODO Funktion database.show_tables(cursor)
print(str(cursor.fetchall()).replace(',', ''))
return return
case 11: case 11:
table_name = str(input('Bitte den Tabellennamen angeben\n')) table_name = str(input('Bitte den Tabellennamen angeben\n'))

View File

@ -5,7 +5,7 @@ import database_menu
def main_menu(): def main_menu():
enable_menu = True 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') print('# Main Menü #\n')
while enable_menu: while enable_menu:
@ -20,5 +20,4 @@ def main_menu():
# TODO Starte Zufallsfragenausgabe # TODO Starte Zufallsfragenausgabe
return return
case 2: case 2:
# TODO Programm Beenden break
return