Python Forum
[mysql.connector] The database is never connected to and there are no errors
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[mysql.connector] The database is never connected to and there are no errors
#6
(Jun-04-2020, 06:56 PM)buran Wrote: table is still tuple
Oh, 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
Reply


Messages In This Thread
RE: [mysql.connector] The database is never connected to and there are no errors - by SheeppOSU - Jun-04-2020, 07:52 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Display name of a pc connected over lan fatopeo 1 1,448 Mar-16-2022, 10:10 AM
Last Post: Gribouillis
  mysql connector/telnet issue (re: text game) rebubula76 1 2,579 Feb-06-2018, 08:00 PM
Last Post: rebubula76

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020