18.3.2022
This commit is contained in:
parent
0768bb7782
commit
356b1a7f7c
26
database.py
26
database.py
|
@ -5,18 +5,14 @@ import mysql.connector
|
|||
|
||||
# Create Database or Table
|
||||
def create(db_cursor, name, type_):
|
||||
query = f'CREATE {type_} {name}'
|
||||
query = f'CREATE {type_} {name};'
|
||||
print(query)
|
||||
try:
|
||||
db_cursor.execute(query)
|
||||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
|
||||
|
||||
def add(db_cursor, name, type_):
|
||||
# TODO
|
||||
return
|
||||
|
||||
|
||||
def read(db_cursor, name, type_):
|
||||
# TODO
|
||||
return
|
||||
|
@ -28,10 +24,26 @@ def update(db_cursor, name, type_):
|
|||
|
||||
|
||||
def del_(db_cursor, name, type_):
|
||||
query = f'DROP if exists {type_} {name}'
|
||||
query = f'DROP {type_} IF EXISTS {name};'
|
||||
try:
|
||||
db_cursor.execute(query)
|
||||
except mysql.connector.Error as err:
|
||||
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 show(db_cursor, type_):
|
||||
query = f'SHOW {type_};'
|
||||
try:
|
||||
db_cursor.execute(query)
|
||||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
# TODO Database verbindung herstellen
|
||||
|
||||
|
||||
import mysql.connector
|
||||
|
||||
|
||||
# Aufbau der verbindung zum Mysql Server
|
||||
|
||||
|
||||
def db_connection(user, password, host):
|
||||
connection = ''
|
||||
|
||||
|
@ -13,4 +15,5 @@ def db_connection(user, password, host):
|
|||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
print('Veruche deinen Mysql Server neu zu starten')
|
||||
return connection.cursor()
|
||||
return connection
|
||||
|
||||
|
|
|
@ -6,32 +6,38 @@ import main_menu
|
|||
|
||||
# Datenbank Menü
|
||||
def menu():
|
||||
enable_menu = True
|
||||
|
||||
selection = {0: 'Datenbanken anzeigen', 1: 'Datenbank erstellen', 2: 'Datenbank löschen', 3: 'Datenbank umbenennen',
|
||||
4: 'Datenbank auselesen',
|
||||
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: 'Zurück zum Hauptmenü', 12 : 'Frage/Antwort hinzufügen'}
|
||||
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 enable_menu:
|
||||
cursor = database_connection.db_connection(config.user, config.password, config.host)
|
||||
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)
|
||||
selection_number = int(input('Bitte Auswahl treffen'))
|
||||
selection_number = int(input('\nBitte Auswahl treffen\n'))
|
||||
|
||||
match selection_number:
|
||||
case 0:
|
||||
use_type = 'DATABASES'
|
||||
database.show(cursor, use_type)
|
||||
print('# Dataenbanken #')
|
||||
print(str(cursor.fetchall()).replace(',', ''))
|
||||
case 1:
|
||||
_type = 'DATABASE'
|
||||
print('#Datenbank erstellen#')
|
||||
database_name = input('Wie soll die Datenbank heißen?')
|
||||
database.create(cursor, database_name, _type)
|
||||
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:
|
||||
_type = 'DATABASE'
|
||||
print('#Datenbank löschen#') # Platzhalter für die Funktion
|
||||
database_name = input('Welche Datenbank soll gelöscht werden?')
|
||||
database.del_(cursor, database_name, _type)
|
||||
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:
|
||||
# TODO Funktion
|
||||
return
|
||||
|
@ -39,10 +45,10 @@ def menu():
|
|||
# TODO Funktion
|
||||
return
|
||||
case 5:
|
||||
_type = 'TABLE'
|
||||
print('#Tabelle erstellen#')
|
||||
table_name = input('Wie soll die Datenbank heißen?')
|
||||
database.create(cursor, table_name, _type)
|
||||
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:
|
||||
# TODO Funktion
|
||||
return
|
||||
|
@ -59,9 +65,12 @@ def menu():
|
|||
# TODO Funktion
|
||||
return
|
||||
case 11:
|
||||
main_menu.main_menu()
|
||||
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:
|
||||
# TODO Funktion
|
||||
return
|
||||
main_menu.main_menu()
|
||||
|
||||
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue
Block a user