(Jun-04-2020, 06:56 PM)buran Wrote: table is still tupleOh, right, I forgot about that part, I'll be sure to change that.
(Jun-04-2020, 06:54 PM)buran Wrote: pass tables as argument to __init__In that case I'll define it inside server.py and pass it into the DataBase init.
So this is the full code (settings contains the same ip and port shown in the original code). Also note that I have yet to test the class functions, I was trying to go error by error.
import mysql.connector as conn class DataBase: def __init__(self, settings, tables): try: print("Looking for database") self.db = conn.connect(host=settings["IP"], port=settings["PORT"], user="***", passwd="***", database="Socket_Online_Games_Database") print("Found already existing database") self.cursor = self.db.cursor() cursor.execute("SHOW TABLES") for table in cursor: if table[0] not in tables: self.cursor.execute("CREATE TABLE %s (%s)" % (table[0], ", ".join(tables[table[0]]))) except Exception as err: print(err) self.db = conn.connect(host=settings["IP"], port=settings["PORT"], user="***", passwd="***") self.cursor = self.db.cursor() self.cursor.execute("CREATE DATABASE Socket_Online_Games_Database") print("Made new database") for table in tables: self.cursor.execute("CREATE TABLE %s (%s)" %(table, ", ".join(tables[table]))) def insert_column(self, table, values): valueNames = ''.join(str(values[0]).split('\'')) cmdDict = {True: self.cursor.execute, False: self.cursor.executemany} cmdDict[len(values)==1](f"INSERT INTO {valueNames} VALUES ({', '.join('%s'*len(values[0]))})", values[1:]) self.db.commit() def get_column(self, table, columns, filter='', limit='', limitOffset=''): if filter != '': filter = f" WHERE {filter[0]} ='{filter[1]}'" if limit != '': limit = f" LIMIT {str(limit)}" if limitOffset != '': limitOffset = f" OFFSET {str(limitOffset)}" self.cursor.execute(f"SELECT {', '.join(columns)} FROM {table}" + filter + limit + limitOffset) return self.cursor.fetchall() def delete_column(self, table, filter): self.cursor.execute(f"DELETE FROM {table} WHERE {filter[0]} = '{filter[1]}'") self.db.commit() def delete_table(self, table): self.cursor.execute("DROP TALE IF EXISTS %s" %table) def update_column(self, table, columnInfo): self.cursor.execute(f"UPDATE {table} SET {columnInfo[0]} = '{columnInfo[2]}' WHERE {columnInfo[0]} = '{columnInfo[1]}'") self.db.commit()As for the original error I'll continue to see if I can solve it myself. Thanks for helping.
Edit: Forgot about the username and password thank you for removing it