fix: database

This commit is contained in:
starnakin 2023-06-04 12:31:08 +02:00
parent 9958c7232f
commit b9f10402ed

View File

@ -1,6 +1,12 @@
import mysql.connector import mysql.connector
import User import User
def is_in(lst: list, name: str):
for i in lst:
if (i[0] == name):
return (1);
return (0);
class Database: class Database:
def __init__(self, host, port, user, password): def __init__(self, host, port, user, password):
""" """
@ -21,7 +27,7 @@ class Database:
) )
self.cursor = self.mydb.cursor() self.cursor = self.mydb.cursor()
self.cursor.execute("SHOW DATABASES"); self.cursor.execute("SHOW DATABASES");
if (not "zeolak" in self.cursor): if (not is_in(self.cursor, "zeolak")):
self.cursor.execute("CREATE DATABASE zeolak") self.cursor.execute("CREATE DATABASE zeolak")
self.mydb = mysql.connector.connect( self.mydb = mysql.connector.connect(
host = host, host = host,
@ -34,14 +40,16 @@ class Database:
def _create_table(self, name: str, content: dict): def _create_table(self, name: str, content: dict):
command: str = f"CREATE TABLE {name} (" command: str = f"CREATE TABLE {name} ("
for item in content.keys(): for key, value in content.items():
command += f"{item[0]} {item[1]}, " command += f"{key} {value}, "
command = command[:-2]
command += ")" command += ")"
print (command)
self.cursor.execute(command) self.cursor.execute(command)
def create_user_table(self): def create_user_table(self):
self.cursor.execute("SHOW TABLES"); self.cursor.execute("SHOW TABLES");
if (not "users" in self.cursor): if (not is_in(self.cursor, "users")):
data = { data = {
"email": "VARCHAR(255)", "email": "VARCHAR(255)",
"username": "VARCHAR(255)", "username": "VARCHAR(255)",