[DB-SIG] new to list - subclass of 'MySQLdb'??

Hardy Merrill hmerrill at redhat.com
Wed Nov 19 10:17:40 EST 2003


I'm new to this list, and just started experimenting with
database access with Python.  I come from the Perl world
where Perl DBI is the "standard" for database access in
Perl.

I've chosen MySQL to start with, so I downloaded
MySQLdb and installed it(from source).  So, noticing that
I have to 'import MySQLdb' and then refer to MySQLdb-specific
functions, like

   MySQLdb.connect(blah blah)

I thought I'd try to find or write a database independent
layer so that I could do something like

   MyDB.py
   -------
   #!/usr/bin/python
    
   import MySQLdb
     
   class MyDB(MySQLdb):
       def __init__(self):
           MySQLdb.__init__(self)

   test.py
   -------
   #!/usr/bin/python
   import MyDB
   mydb = MyDB()
   conn = MyDB.connect(db="mydatabase", user="myuser", passwd="mypass")

to make the code more generic and more portable if I were
to change the database in the future.

This doesn't work - when I run test.py I get this error:

   [hmerrill at merrill mysqldb]$ ./test.py 
   Traceback (most recent call last):
     File "./test.py", line 3, in ?
       mydb = MyDB()
   TypeError: 'module' object is not callable

Am I doing this wrong?  How *should* I be doing this?  Anyone
have some similar code they can show me to start me off in
the right direction for subclassing MySQLdb to create a db
independent interface to the MySQLdb module?

TIA.

-- 
Hardy Merrill
Red Hat, Inc.



More information about the DB-SIG mailing list