how to mysqldb dict cursors

Steve Holden steve at holdenweb.com
Fri Apr 25 07:38:08 EDT 2008


Vaibhav.bhawsar wrote:
[top-posting amended: see below]
> On Fri, Apr 25, 2008 at 12:45 AM, Paul McNett <p at ulmcnett.com 
> <mailto:p at ulmcnett.com>> wrote:
> 
>     Vaibhav.bhawsar wrote:
> 
>         I have been trying to get the DictCursor working with mysqldb
>         module but can't seem to. I have pasted the basic connection
>         code and the traceback from pydev. The connection does open with
>         the default cursor class. can't figure this one out. many thanks.
> 
> 
>     Try one of:
> 
>     """
>     import MySQLdb, MySQLdb.cursors
>     conn = MySQLdb.connect(..., cursorclass=MySQLdb.cursors.DictCursor)
>     """
> 
>     -or-
> 
>     """
>     import MySQLdb, MySQLdb.cursors
>     conn = MySQLdb.connect(...)
>     cur = MySQLdb.cursors.DictCursor(conn)
>     """
> 
>     I'm going off of memory here, though, but I'm at least close.
> 
 > Great both methods worked! I don't quite understand this since i 
imported
 > the whole module with "import MySQLdb"
 >
 > Thanks!
 >
The point here is that MySQLdb is a package, not a module. Some packages 
have their top-level __init__.py import the package's sub-modules or 
sub-packages to make them immediately available within the package 
namespace (which is why, for example, you can access os.path.* when you 
have imported os) and others don't.

MySQLdb clearly doesn't need to import the cursors module for its own 
purposes. Perhaps it would be less efficient to always perfrom the 
import, who knows. Well, Andy Dustman does, I suppose, and possibly 
anyone else who reads the code, but I haven't done that myself.

regards
  Steve
-- 
Steve Holden        +1 571 484 6266   +1 800 494 3119
Holden Web LLC              http://www.holdenweb.com/




More information about the Python-list mailing list