24.03.2022 Database & Menu update
This commit is contained in:
parent
089f180ac4
commit
4ed70bb1d5
|
@ -1,7 +1,7 @@
|
||||||
# TODO: Datenbank Klasse (CRUD)
|
# TODO: Datenbank Klasse (CRUD)
|
||||||
#
|
#
|
||||||
# TODO: Tabellen und Spaltennamen selbst bestimmen
|
# TODO: Tabellen und Spaltennamen selbst bestimmen
|
||||||
|
import re
|
||||||
|
|
||||||
import mysql.connector
|
import mysql.connector
|
||||||
|
|
||||||
|
@ -11,13 +11,14 @@ def create(db_cursor, name, type_):
|
||||||
# TODO Fix nötig
|
# TODO Fix nötig
|
||||||
query = f'CREATE {type_} {name};'
|
query = f'CREATE {type_} {name};'
|
||||||
if type_ == 'TABLE':
|
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:
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
# Datenbank auslesen
|
||||||
def read(db_cursor, name):
|
def read(db_cursor, name):
|
||||||
query = f'SELECT * FROM {name}'
|
query = f'SELECT * FROM {name}'
|
||||||
try:
|
try:
|
||||||
|
@ -26,11 +27,12 @@ def read(db_cursor, name):
|
||||||
print('Something went wrong', err)
|
print('Something went wrong', err)
|
||||||
|
|
||||||
|
|
||||||
def update(db_cursor, name, type_):
|
|
||||||
# TODO
|
# TODO
|
||||||
|
def update(db_cursor, name, type_):
|
||||||
return
|
return
|
||||||
|
|
||||||
|
|
||||||
|
# Löschen von Datenbank oder Tabelle
|
||||||
def del_(db_cursor, name, type_):
|
def del_(db_cursor, name, type_):
|
||||||
query = f'DROP {type_} IF EXISTS {name};'
|
query = f'DROP {type_} IF EXISTS {name};'
|
||||||
try:
|
try:
|
||||||
|
@ -38,23 +40,26 @@ def del_(db_cursor, name, type_):
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
print('Something went wrong', err)
|
print('Something went wrong', err)
|
||||||
|
|
||||||
# TODO hier die Spaltennamen selbst bestimmen
|
|
||||||
def add(db_cursor, name, question, answer):
|
# Spalten anlegen
|
||||||
query = f'INSERT INTO {name} (question, answer) VALUES ({question}, {answer});'
|
def add_columns(db_cursor, column_name, table_name):
|
||||||
|
query = f'INSERT INTO {table_name} {column_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 show(db_cursor, type_):
|
# TODO
|
||||||
query = f'SHOW {type_};'
|
def add_values(db_cursor, table_name, column_name, value):
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
# Datenbank benutzen
|
||||||
def use(db_cursor, name):
|
def use(db_cursor, name):
|
||||||
query = f'USE {name};'
|
query = f'USE {name};'
|
||||||
try:
|
try:
|
||||||
|
@ -63,25 +68,14 @@ def use(db_cursor, name):
|
||||||
print('Something went wrong', err)
|
print('Something went wrong', err)
|
||||||
|
|
||||||
|
|
||||||
def show_tables(db_cursor):
|
# Zeigt Datenbanken oder Tabellen in der ausgewähltem Datenbank anzeigen
|
||||||
query = 'show tables;'
|
def show_all(db_cursor, use_type):
|
||||||
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}'
|
query = f'show {use_type}'
|
||||||
try:
|
try:
|
||||||
db_cursor.execute(query)
|
db_cursor.execute(query)
|
||||||
for i in db_cursor.fetchall():
|
for i in db_cursor.fetchall():
|
||||||
print(str(i).replace(',','').replace('(', '').replace(')', ''))
|
string_ = re.sub('[,()]', '', str(i))
|
||||||
|
print(string_)
|
||||||
print('\n')
|
print('\n')
|
||||||
except mysql.connector.Error as err:
|
except mysql.connector.Error as err:
|
||||||
print('Something went wrong', err)
|
print('Something went wrong', err)
|
||||||
|
|
||||||
|
|
||||||
def string_formater(_string):
|
|
||||||
_string.replace(',','').replace('(', '').replace(')', '')
|
|
||||||
return str(_string)
|
|
|
@ -12,7 +12,7 @@ def menu():
|
||||||
3: ' Datenbank umbenennen',
|
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',
|
9: ' Spalten erstellen', 10: 'Tabellen anzeigen', 11: 'Frage/Antwort hinzufügen',
|
||||||
12: 'Zurück zum Hauptmenü'}
|
12: 'Zurück zum Hauptmenü'}
|
||||||
|
|
||||||
while connection.is_connected():
|
while connection.is_connected():
|
||||||
|
@ -28,7 +28,7 @@ def menu():
|
||||||
case 0:
|
case 0:
|
||||||
use_type = 'DATABASES'
|
use_type = 'DATABASES'
|
||||||
print('# Datenbanken #\n')
|
print('# Datenbanken #\n')
|
||||||
database.show(cursor, use_type)
|
database.show_all(cursor, use_type)
|
||||||
case 1:
|
case 1:
|
||||||
use_type = 'DATABASE'
|
use_type = 'DATABASE'
|
||||||
print('# Datenbank erstellen #\n')
|
print('# Datenbank erstellen #\n')
|
||||||
|
@ -36,7 +36,7 @@ def menu():
|
||||||
database.create(cursor, database_name, use_type)
|
database.create(cursor, database_name, use_type)
|
||||||
case 2:
|
case 2:
|
||||||
use_type = 'DATABASE'
|
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_name = input('Welche Datenbank soll gelöscht werden?\n')
|
||||||
database.del_(cursor, database_name, use_type)
|
database.del_(cursor, database_name, use_type)
|
||||||
case 3:
|
case 3:
|
||||||
|
@ -61,19 +61,22 @@ def menu():
|
||||||
print('# Tabelle Auslesen #\n')
|
print('# Tabelle Auslesen #\n')
|
||||||
name = input('Aus welcher Tabelle soll dir alles angezeigt werden?\n')
|
name = input('Aus welcher Tabelle soll dir alles angezeigt werden?\n')
|
||||||
database.read(cursor, name)
|
database.read(cursor, name)
|
||||||
|
|
||||||
case 9:
|
case 9:
|
||||||
print('--Funktion ist in Arbeit-- \n')
|
print('# Spalten erstellen #\n')
|
||||||
continue
|
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:
|
case 10:
|
||||||
database.show_tables(cursor)
|
print(' # Tabellen anzeigen #\n ')
|
||||||
print(str(cursor.fetchall()).replace(',', ''))
|
use_type = 'TABLES'
|
||||||
return
|
database.show_all(cursor, use_type)
|
||||||
case 11:
|
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")
|
print("#Fragen und Antworten hinzufügen #\n")
|
||||||
question = str(input('Bitte die Frage eingeben\n'))
|
question = str(input('Bitte die Frage eingeben\n'))
|
||||||
answer = str(input('Bitte die dazugehörige antwort 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:
|
case 12:
|
||||||
main_menu.main_menu()
|
main_menu.main_menu()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user