Python class to take care of MySQL connection keeps throwing an error
<p>I am trying to build a class using Python in order to handle all interactions with MySQL databases. 
I have installed the MySQLdb module and tested the connection to the databases using a simple code.
When I run the following code:</p>

<pre><code>import MySQLdb

db = MySQLdb.connect ( host="localhost", user="root" , passwd="xxxxxxx" , db= "some_db")
cursor = db.cursor()
s = cursor.execute ("SELECT * FROM some_table")
#Retrive a record to test it all went well...
t = cursor.fetchall()
for x in t:
print x
db.commit()
db.close()
</code></pre>

<p>The above code worked fine , it connected to the "some_db" database retrieved the one  record from the "some_table" table.</p>

<p>However, When I run the code listed below , which consists  of a section of a class using the same logic  , I get an error thrown . The idea behind the class is to open any database and as many connections as one might wish to open, perform queries etc  . Please bear in mind that the following is just a section of class, more functions and what-have- you will be added. </p>

<p>The code is as follows:</p>

<pre><code>import MySQLdb

class d_con (object):
    def __init__ (self, host, user , passwd , db ):
        self.host = host
        self.user = user
        self.passwd = passwd
        self.db = db
        self.d_b = MySQLdb.connect( host = self.host , user = self.user ,  passwd = self.passwd , db = self.db )
        #Test the connection...
        self.cur = self.d_b.cursor()
        self.disp = self.cur.execute ("SELECT VERSION()")
        print self.disp

    def close_connection(self):
        print "...Initializing connection closure..."
        self.d_b.close()

    #More function will be added , I am just testing the constructor.

if __name__ == "__main__ " :
    a = d_con()

else: 
    print ( " d_con class is imported , please instantiate an object in the fashion a = datacon.d_con() . ")
</code></pre>

<p>When I import the file containing the above code, it executes and prints the statement asking to instantiate the class. After I instantiate the class</p>

<pre><code>a=datacon.d_con("localhost" , "root" , "xxxxxxx" , "some_db" ) 
</code></pre>

<p>this  error get thrown:</p>

<pre><code>Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "datacon.py", line 8, in __init__
    self.d_b = MySQLdb.connect( self.host , self.user ,  self.passwd ,  self.db )
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-   packages/MySQLdb/connections.py", line 193, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
_mysql_exceptions.OperationalError: (1049, "Unknown database 'some_table'")
</code></pre>

<p>Rest assured that I have, repeatedly,  checked that the parameters of the MySQLdb.connect () are correct. Besides, I, even,  have hardwired the parameters in the constructor to make sure that there are no typos when instantiating the class. Every time, I have the same error.</p>

<p>Since I am new to Python , I am not sure whether the   class file  has trouble importing the MySQLdb module or if there is a problem of variable scope . The MySQLdb module did import successfully when I executed the code at the top of this conversation using the same database and the same MySQL credentials!
Could you guys help with this?
Thanks in advance. </p>

<p>Ps: I am running all this on Mac Os 10.12</p>

<p>Sorry guys! The code is fine, I was, systematically, inputing the name of a table instead of a database to the self.db variable .
The code works fine.</p>

python mysql python-2.7 class mysql-python