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
|
# Create Database or Table
|
||||||
def create(db_cursor, name, type_):
|
def create(db_cursor, name, type_):
|
||||||
query = f'CREATE {type_} {name}'
|
query = f'CREATE {type_} {name};'
|
||||||
|
print(query)
|
||||||
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 add(db_cursor, name, type_):
|
|
||||||
# TODO
|
|
||||||
return
|
|
||||||
|
|
||||||
|
|
||||||
def read(db_cursor, name, type_):
|
def read(db_cursor, name, type_):
|
||||||
# TODO
|
# TODO
|
||||||
return
|
return
|
||||||
|
@ -28,10 +24,26 @@ def update(db_cursor, name, type_):
|
||||||
|
|
||||||
|
|
||||||
def del_(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:
|
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 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
|
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):
|
||||||
connection = ''
|
connection = ''
|
||||||
|
|
||||||
|
@ -13,4 +15,5 @@ def db_connection(user, password, host):
|
||||||
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')
|
||||||
return connection.cursor()
|
return connection
|
||||||
|
|
||||||
|
|
|
@ -6,32 +6,38 @@ import main_menu
|
||||||
|
|
||||||
# Datenbank Menü
|
# Datenbank Menü
|
||||||
def menu():
|
def menu():
|
||||||
enable_menu = True
|
|
||||||
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: 'Datenbank auselesen',
|
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: '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'
|
print('#Datenbank Menü# \n'
|
||||||
'\nWas möchtest du tun?\n')
|
'\nWas möchtest du tun?\n')
|
||||||
|
|
||||||
while enable_menu:
|
while True:
|
||||||
cursor = database_connection.db_connection(config.user, config.password, config.host)
|
connection = database_connection.db_connection(config.user, config.password, config.host)
|
||||||
|
cursor = connection.cursor()
|
||||||
for key, value in selection.items():
|
for key, value in selection.items():
|
||||||
print(key, value)
|
print(key, value)
|
||||||
selection_number = int(input('Bitte Auswahl treffen'))
|
selection_number = int(input('\nBitte Auswahl treffen\n'))
|
||||||
|
|
||||||
match selection_number:
|
match selection_number:
|
||||||
|
case 0:
|
||||||
|
use_type = 'DATABASES'
|
||||||
|
database.show(cursor, use_type)
|
||||||
|
print('# Dataenbanken #')
|
||||||
|
print(str(cursor.fetchall()).replace(',', ''))
|
||||||
case 1:
|
case 1:
|
||||||
_type = 'DATABASE'
|
use_type = 'DATABASE'
|
||||||
print('#Datenbank erstellen#')
|
print('# Datenbank erstellen #\n')
|
||||||
database_name = input('Wie soll die Datenbank heißen?')
|
database_name = input('Wie soll die Datenbank heißen?\n')
|
||||||
database.create(cursor, database_name, _type)
|
database.create(cursor, database_name, use_type)
|
||||||
case 2:
|
case 2:
|
||||||
_type = 'DATABASE'
|
use_type = 'DATABASE'
|
||||||
print('#Datenbank löschen#') # Platzhalter für die Funktion
|
print('# Datenbank löschen #\n') # Platzhalter für die Funktion
|
||||||
database_name = input('Welche Datenbank soll gelöscht werden?')
|
database_name = input('Welche Datenbank soll gelöscht werden?\n')
|
||||||
database.del_(cursor, database_name, _type)
|
database.del_(cursor, database_name, use_type)
|
||||||
case 3:
|
case 3:
|
||||||
# TODO Funktion
|
# TODO Funktion
|
||||||
return
|
return
|
||||||
|
@ -39,10 +45,10 @@ def menu():
|
||||||
# TODO Funktion
|
# TODO Funktion
|
||||||
return
|
return
|
||||||
case 5:
|
case 5:
|
||||||
_type = 'TABLE'
|
use_type = 'TABLE'
|
||||||
print('#Tabelle erstellen#')
|
print('# Tabelle erstellen #\n')
|
||||||
table_name = input('Wie soll die Datenbank heißen?')
|
table_name = input('Wie soll die Datenbank heißen?\n')
|
||||||
database.create(cursor, table_name, _type)
|
database.create(cursor, table_name, use_type)
|
||||||
case 6:
|
case 6:
|
||||||
# TODO Funktion
|
# TODO Funktion
|
||||||
return
|
return
|
||||||
|
@ -59,9 +65,12 @@ def menu():
|
||||||
# TODO Funktion
|
# TODO Funktion
|
||||||
return
|
return
|
||||||
case 11:
|
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:
|
case 12:
|
||||||
# TODO Funktion
|
main_menu.main_menu()
|
||||||
return
|
|
||||||
|
|
||||||
break
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user