19.3.2022
This commit is contained in:
parent
356b1a7f7c
commit
25be3b8739
|
@ -1,6 +1,6 @@
|
|||
# TODO Database verbindung herstellen
|
||||
|
||||
# Database Verbinungsdaten
|
||||
# Database Verbindungsdaten
|
||||
|
||||
user = 'root'
|
||||
password = ''
|
||||
|
|
37
database.py
37
database.py
|
@ -5,17 +5,22 @@ import mysql.connector
|
|||
|
||||
# Create Database or Table
|
||||
def create(db_cursor, name, type_):
|
||||
# TODO Fix nötig
|
||||
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:
|
||||
db_cursor.execute(query)
|
||||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
|
||||
|
||||
def read(db_cursor, name, type_):
|
||||
# TODO
|
||||
return
|
||||
def read(db_cursor, name):
|
||||
query = f'SELECT * FROM {name}'
|
||||
try:
|
||||
db_cursor.execute(query)
|
||||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
|
||||
|
||||
def update(db_cursor, name, type_):
|
||||
|
@ -31,11 +36,12 @@ def del_(db_cursor, name, type_):
|
|||
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 add(db_cursor, name, question, answer):
|
||||
query = f'INSERT INTO {name} (question, answer) VALUES ({question}, {answer});'
|
||||
try:
|
||||
db_cursor.execute(query)
|
||||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
|
||||
|
||||
def show(db_cursor, type_):
|
||||
|
@ -46,4 +52,17 @@ def show(db_cursor, type_):
|
|||
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)
|
|
@ -6,12 +6,12 @@ import mysql.connector
|
|||
# Aufbau der verbindung zum Mysql Server
|
||||
|
||||
|
||||
def db_connection(user, password, host):
|
||||
def db_connection(user, password, host, database):
|
||||
connection = ''
|
||||
|
||||
try:
|
||||
# Verbindung mit die in der Config festgelegt werden
|
||||
connection = mysql.connector.connect(user=user, password=password, host=host)
|
||||
# Verbindung - Eingaben in der config.py bestimmen
|
||||
connection = mysql.connector.connect(user=user, password=password, host=host, database=database)
|
||||
except mysql.connector.Error as err:
|
||||
print('Something went wrong', err)
|
||||
print('Veruche deinen Mysql Server neu zu starten')
|
||||
|
|
|
@ -6,6 +6,7 @@ import main_menu
|
|||
|
||||
# Datenbank Menü
|
||||
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',
|
||||
4: ' Datenbanken benutzen',
|
||||
|
@ -16,7 +17,7 @@ def menu():
|
|||
'\nWas möchtest du tun?\n')
|
||||
|
||||
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)
|
||||
|
@ -26,7 +27,7 @@ def menu():
|
|||
case 0:
|
||||
use_type = 'DATABASES'
|
||||
database.show(cursor, use_type)
|
||||
print('# Dataenbanken #')
|
||||
print('# Datenbanken #')
|
||||
print(str(cursor.fetchall()).replace(',', ' '))
|
||||
case 1:
|
||||
use_type = 'DATABASE'
|
||||
|
@ -42,8 +43,9 @@ def menu():
|
|||
# TODO Funktion
|
||||
return
|
||||
case 4:
|
||||
# TODO Funktion
|
||||
return
|
||||
name = input('Welche Datenbank möchtest du benutzen?\n')
|
||||
database.use(cursor, name)
|
||||
connection.database = name
|
||||
case 5:
|
||||
use_type = 'TABLE'
|
||||
print('# Tabelle erstellen #\n')
|
||||
|
@ -56,13 +58,16 @@ def menu():
|
|||
# TODO Funktion
|
||||
return
|
||||
case 8:
|
||||
# TODO Funktion
|
||||
return
|
||||
print('# Tabelle Auslesen #\n')
|
||||
name = input('Aus welcher Tabelle soll dir alles angezeigt werden?\n')
|
||||
database.read(cursor, name)
|
||||
|
||||
case 9:
|
||||
# TODO Funktion
|
||||
return
|
||||
case 10:
|
||||
# TODO Funktion
|
||||
database.show_tables(cursor)
|
||||
print(str(cursor.fetchall()).replace(',', ''))
|
||||
return
|
||||
case 11:
|
||||
table_name = str(input('Bitte den Tabellennamen angeben\n'))
|
||||
|
|
|
@ -5,7 +5,7 @@ import database_menu
|
|||
|
||||
def main_menu():
|
||||
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')
|
||||
while enable_menu:
|
||||
|
@ -20,5 +20,4 @@ def main_menu():
|
|||
# TODO Starte Zufallsfragenausgabe
|
||||
return
|
||||
case 2:
|
||||
# TODO Programm Beenden
|
||||
return
|
||||
break
|
||||
|
|
Loading…
Reference in New Issue
Block a user