deriving MySQLdb class

T

tekion

All,
I am trying to write a class which inherits from MySQLdb class. Below
is code snippet:
import MySQLdb
import sys

class msql_connect(MySQLdb):
def __init__(self):
self.host = "hostname"
self.user = "user"
self.password = "passoword"
self.database = "database name"

I am running into below error:
class msql_connect(MySQLdb):
TypeError: Error when calling the metaclass bases
module.__init__() takes at most 2 arguments (3 given)


Does any one have an idea why? Thanks.
 
S

Sean DiZazzo

All,
I am trying to write a class which inherits from MySQLdb class.  Below
is code snippet:
import MySQLdb
import sys

class  msql_connect(MySQLdb):
    def __init__(self):
        self.host     =  "hostname"
        self.user     = "user"
        self.password  = "passoword"
        self.database = "database name"

I am running into below error:
 class  msql_connect(MySQLdb):
TypeError: Error when calling the metaclass bases
    module.__init__() takes at most 2 arguments (3 given)

Does any one have an idea why?  Thanks.

MySQLdb is the name of the module, not the class you want to
subclass. But MySQLdb.connect() is not the class either...it's a
factory function that returns instances of the class you actually want
to subclass...connections.Connection(). The below works for me.

from MySQLdb import connections
import sys

class mysql_connect(connections.Connection):
def __init__(self):
self.host = "host"
self.user = "user"
self.password = "password"
self.database = "database"
connections.Connection.__init__(self, host=self.host,
user=self.user, passwd=self.password, db=self.database)

p = mysql_connect()

~Sean
 
T

tekion

Sean,
Thanks. This is useful. For future reference, how do I know what
class is in MySQLdb module?
 
S

Sean DiZazzo

Sean,
Thanks.  This is useful.  For future reference, how do I know what
class is in MySQLdb module?

You have to explore. ;)

I found the MySQLdb module, and looked inside the __init__.py. Then
looked for "connect" and followed the trail.
 
T

tekion

Sean,
I did a little investigation, there are other classes besides
Connection. So, could I only set up a derived class from Connection
and still be able to use the connection to query database and retrieve
data?
 
S

Sean DiZazzo

Sean,
I did a little investigation, there are other classes besides
Connection. So, could I only set up a derived class from Connection
and still be able to use the connection to query database and retrieve
data?

Im not sure I understand you completely...

In theory, you could use the C API directly and subclass
_mysql.connection to get at the database. But I think the point of
MySQLdb is that they've done all the hard work. Why not use it?

I think the other stuff in the module is in support of the Connection
() class. ie. You cant get a cursor unless you already have a
connection.
 
T

tekion

Im not sure I understand you completely...

In theory, you could use the C API directly and subclass
_mysql.connection to get at the database.  But I think the point of
MySQLdb is that they've done all the hard work.  Why not use it?

Regarding above statement, Yeah; what you said is true. I am just
playing with inheritance to get a better understanding of it and since
I am working with database connection, then I might as learn it by
doing it.
I think the other stuff in the module is in support of the Connection
() class.  ie.  You cant get a cursor unless you already have a
connection.
Yes, I have confirmed it just by inheriting the connection class, I am
able to connect and perform normal database functions. Sweet!!! This
is why I like Python.

Thanks for your help.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top