24.03.2022 Database & Menu update

This commit is contained in:
marcus 2022-03-24 10:41:17 +01:00
parent 089f180ac4
commit 4ed70bb1d5
2 changed files with 31 additions and 34 deletions

View File

@ -1,7 +1,7 @@
# TODO: Datenbank Klasse (CRUD)
#
# TODO: Tabellen und Spaltennamen selbst bestimmen
import re
import mysql.connector
@ -11,13 +11,14 @@ 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));'
query = f'CREATE {type_} {name}'
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)
# Datenbank auslesen
def read(db_cursor, name):
query = f'SELECT * FROM {name}'
try:
@ -26,11 +27,12 @@ def read(db_cursor, name):
print('Something went wrong', err)
# TODO
def update(db_cursor, name, type_):
# TODO
return
# Löschen von Datenbank oder Tabelle
def del_(db_cursor, name, type_):
query = f'DROP {type_} IF EXISTS {name};'
try:
@ -38,23 +40,26 @@ def del_(db_cursor, name, type_):
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});'
# Spalten anlegen
def add_columns(db_cursor, column_name, table_name):
query = f'INSERT INTO {table_name} {column_name};'
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_};'
# TODO
def add_values(db_cursor, table_name, column_name, value):
query = ''
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)
# Datenbank benutzen
def use(db_cursor, name):
query = f'USE {name};'
try:
@ -63,25 +68,14 @@ def use(db_cursor, name):
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):
# Zeigt Datenbanken oder Tabellen in der ausgewähltem Datenbank anzeigen
def show_all(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(')', ''))
string_ = re.sub('[,()]', '', str(i))
print(string_)
print('\n')
except mysql.connector.Error as err:
print('Something went wrong', err)
def string_formater(_string):
_string.replace(',','').replace('(', '').replace(')', '')
return str(_string)

View File

@ -12,7 +12,7 @@ def menu():
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',
9: ' Spalten erstellen', 10: 'Tabellen anzeigen', 11: 'Frage/Antwort hinzufügen',
12: 'Zurück zum Hauptmenü'}
while connection.is_connected():
@ -28,7 +28,7 @@ def menu():
case 0:
use_type = 'DATABASES'
print('# Datenbanken #\n')
database.show(cursor, use_type)
database.show_all(cursor, use_type)
case 1:
use_type = 'DATABASE'
print('# Datenbank erstellen #\n')
@ -36,7 +36,7 @@ def menu():
database.create(cursor, database_name, use_type)
case 2:
use_type = 'DATABASE'
print('# Datenbank löschen #\n') # Platzhalter für die Funktion
print('# Datenbank löschen #\n')
database_name = input('Welche Datenbank soll gelöscht werden?\n')
database.del_(cursor, database_name, use_type)
case 3:
@ -61,19 +61,22 @@ def menu():
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
print('# Spalten erstellen #\n')
table_name = input('In welcher Tabelle möchtest du spalten anlegen? Achtung, Tabelle muss vorhanden '
'sein! \n')
column_name = input('Wie soll die Spalte heißen?\n')
database.add_columns(cursor,table_name,column_name)
case 10:
database.show_tables(cursor)
print(str(cursor.fetchall()).replace(',', ''))
return
print(' # Tabellen anzeigen #\n ')
use_type = 'TABLES'
database.show_all(cursor, use_type)
case 11:
table_name = str(input('Bitte den Tabellennamen angeben\n'))
# TODO überarbneitung nötig!
"""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)
database.add(cursor, table_name, question, answer)"""
case 12:
main_menu.main_menu()