deriving MySQLdb class

Discussion in 'Python' started by tekion, Jan 22, 2010.

  1. tekion

    tekion Guest

    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.
     
    tekion, Jan 22, 2010
    #1
    1. Advertising

  2. tekion

    Sean DiZazzo Guest

    On Jan 21, 5:48 pm, tekion <> 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
     
    Sean DiZazzo, Jan 22, 2010
    #2
    1. Advertising

  3. tekion

    tekion Guest

    Sean,
    Thanks. This is useful. For future reference, how do I know what
    class is in MySQLdb module?
     
    tekion, Jan 22, 2010
    #3
  4. tekion

    Sean DiZazzo Guest

    On Jan 21, 8:00 pm, tekion <> 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.
     
    Sean DiZazzo, Jan 22, 2010
    #4
  5. tekion

    tekion Guest

    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?
     
    tekion, Jan 22, 2010
    #5
  6. tekion

    Sean DiZazzo Guest

    On Jan 21, 8:17 pm, tekion <> 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.
     
    Sean DiZazzo, Jan 22, 2010
    #6
  7. tekion

    tekion Guest

    On Jan 21, 11:48 pm, Sean DiZazzo <> wrote:
    > On Jan 21, 8:17 pm, tekion <> 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.
     
    tekion, Jan 28, 2010
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mahesh Devjibhai Dhola

    Problem in deriving custome class from XmlNode

    Mahesh Devjibhai Dhola, Oct 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    501
    Mahesh Devjibhai Dhola
    Oct 15, 2004
  2. Gary Rynearson

    Problem deriving from WebControl Class

    Gary Rynearson, Nov 18, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    427
    Gary Rynearson
    Nov 18, 2005
  3. Matthias Kaeppler
    Replies:
    1
    Views:
    458
    R.F. Pels
    May 22, 2005
  4. Steven T. Hatton

    Deriving a regular class from a template?

    Steven T. Hatton, Apr 23, 2004, in forum: C++
    Replies:
    28
    Views:
    2,218
    Richard Herring
    Apr 28, 2004
  5. markww
    Replies:
    1
    Views:
    299
    Ivan Vecerina
    Aug 22, 2006
Loading...

Share This Page