Pruefungskatalog/database.py

68 lines
1.7 KiB
Python
Raw Normal View History

2022-03-17 13:07:35 +01:00
# TODO: Datenbank Klasse (CRUD)
2022-03-18 09:29:41 +01:00
import mysql.connector
2022-03-17 13:22:44 +01:00
2022-03-18 12:30:09 +01:00
# Create Database or Table
2022-03-18 15:06:02 +01:00
def create(db_cursor, name, type_):
2022-03-19 20:55:12 +01:00
# TODO Fix nötig
2022-03-18 22:07:50 +01:00
query = f'CREATE {type_} {name};'
2022-03-19 20:55:12 +01:00
if type_ == 'TABLE':
query = f'CREATE {type_} {name} (ID integer primary key auto_increment, question varchar(255), answers varchar(255));'
2022-03-18 09:26:05 +01:00
try:
db_cursor.execute(query)
2022-03-18 09:29:41 +01:00
except mysql.connector.Error as err:
2022-03-18 09:26:05 +01:00
print('Something went wrong', err)
2022-03-17 13:07:35 +01:00
2022-03-19 20:55:12 +01:00
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)
2022-03-17 13:07:35 +01:00
2022-03-18 15:06:02 +01:00
def update(db_cursor, name, type_):
# TODO
2022-03-17 13:07:35 +01:00
return
2022-03-18 15:06:02 +01:00
def del_(db_cursor, name, type_):
2022-03-18 22:07:50 +01:00
query = f'DROP {type_} IF EXISTS {name};'
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)
2022-03-19 20:55:12 +01:00
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)
2022-03-18 22:07:50 +01:00
def show(db_cursor, type_):
query = f'SHOW {type_};'
2022-03-18 15:06:02 +01:00
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)
2022-03-19 20:55:12 +01:00
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)
2022-03-18 22:07:50 +01:00
2022-03-19 20:55:12 +01:00
def show_tables(db_cursor):
query = 'show tables;'
try:
db_cursor.execute(query)
except mysql.connector.Error as err:
print('Something went wrong', err)