Gia Duong Duc Minh
Gia Duong Duc Minh

Reputation: 1329

Python cannot connect to MySQL if declare in a class

I'm newbie in Python. I'm trying to use Python to connect MySQL Server. I wrote like guides from MySQL official page, it was OK. But, when I create a connector class, it raised the error "MySQL Connection not available"

Here is my class

import mysql.connector
from mysql.connector import errorcode

##  BEGIN MySQL Connector Class
class MySQLConnector :
    configs = {
        "user":"root",
        "password":"",
        "host":"127.0.0.1",
        "database":"python_db",
        "raise_on_warnings": True
    }
    cursor = None
    connection = None

    ##  BEGIN Constructor
    def __init__(self, configs = {}) :
        if(any(configs)!=False) :
            self.configs = configs
    ##  END Constructor

    ##  BEGIN Open
    def open(self) :
        try:
            self.connection = mysql.connector.connect(self.configs)
        except mysql.connector.Error as err:
            if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
                print("Something is wrong with your user name or password")
            elif err.errno == errorcode.ER_BAD_DB_ERROR:
                print("Database does not exists")
            else:
                print(err)
        finally:
            self.connection.close()
        return self.connection
    ##  END Open

    ##  BEGIN close connection
    def close(self) :
        self.cursor.close()
        self.connection.close()
    ##  END close connection

    ##  BEGIN execute
    def execute(self, query) :
        if(self.connection == None) :
            print("Connection is None")
            return
        self.cursor = self.connection.cursor()
        if(self.cursor!=None) :
            self.cursor.execute(query)
        else:
            print("Cursor is 'None'")
    ##  END execute



##  END MySQL Connector Class

##  BEGIN RUN
objConnect = MySQLConnector()
objConnect.open()
objConnect.execute("SELECT * FROM User")

Please show me the way to solution and explained me why my code has error.

Thanks!


EDITED

Finally, mata and alecxe help me to solve this problem, I don't know which solution to be choosen. I summary here for someone has mistake like me: 1. Remove the finally statement. 2. Using ** in self.connection = mysql.connector.connect(**self.configs)

Upvotes: 1

Views: 575

Answers (2)

mata
mata

Reputation: 69042

Even if you correct the error alecxe pointed out, your code still won't work.

The finally block ensures that each connection is closed before it is returned, no matter wheather there was an exception or not, so the open method only returns closed connections.

Upvotes: 3

alecxe
alecxe

Reputation: 473873

You are passing a dictionary object self.configs into mysql.connector.connect, though, according to docs, you should pass to it user, password and other arguments. Looks like you need to unpack configs:

self.connection = mysql.connector.connect(**self.configs)

Hope this is it.

Upvotes: 3

Related Questions