Pruefungskatalog/database.py
2022-03-22 13:10:45 +01:00

87 lines
2.2 KiB
Python

# TODO: Datenbank Klasse (CRUD)
#
# TODO: Tabellen und Spaltennamen selbst bestimmen
import mysql.connector
# Create Database or Table
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));'
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)
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_):
# TODO
return
def del_(db_cursor, name, type_):
query = f'DROP {type_} IF EXISTS {name};'
try:
db_cursor.execute(query)
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});'
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_};'
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
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)
def show(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(')', ''))
print('\n')
except mysql.connector.Error as err:
print('Something went wrong', err)
def string_formater(_string):
_string.replace(',','').replace('(', '').replace(')', '')
return str(_string)