Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > deriving MySQLdb class

Reply
Thread Tools

deriving MySQLdb class

 
 
tekion
Guest
Posts: n/a
 
      01-22-2010
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.
 
Reply With Quote
 
 
 
 
Sean DiZazzo
Guest
Posts: n/a
 
      01-22-2010
On Jan 21, 5:48*pm, tekion <(E-Mail Removed)> wrote:
> 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
 
Reply With Quote
 
 
 
 
tekion
Guest
Posts: n/a
 
      01-22-2010
Sean,
Thanks. This is useful. For future reference, how do I know what
class is in MySQLdb module?
 
Reply With Quote
 
Sean DiZazzo
Guest
Posts: n/a
 
      01-22-2010
On Jan 21, 8:00*pm, tekion <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
tekion
Guest
Posts: n/a
 
      01-22-2010
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?
 
Reply With Quote
 
Sean DiZazzo
Guest
Posts: n/a
 
      01-22-2010
On Jan 21, 8:17*pm, tekion <(E-Mail Removed)> wrote:
> 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.
 
Reply With Quote
 
tekion
Guest
Posts: n/a
 
      01-28-2010
On Jan 21, 11:48*pm, Sean DiZazzo <(E-Mail Removed)> wrote:
> On Jan 21, 8:17*pm, tekion <(E-Mail Removed)> wrote:
>
> > 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?


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deriving a class from a base class markww C++ 1 08-22-2006 11:55 AM
Problem deriving from WebControl Class Gary Rynearson ASP .Net 0 11-18-2005 03:34 PM
Deriving abstract class from non-abstract class Matthias Kaeppler Java 1 05-22-2005 01:28 PM
Problem in deriving custome class from XmlNode Mahesh Devjibhai Dhola ASP .Net 0 10-15-2004 07:38 AM
Deriving a regular class from a template? Steven T. Hatton C++ 28 04-28-2004 10:22 AM



Advertisments