Pruefungskatalog/database.py
2022-03-19 20:55:12 +01:00

68 lines
1.7 KiB
Python

# TODO: Datenbank Klasse (CRUD)
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)
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)