Discussion:
how to mysqldb dict cursors
Paul McNett
2008-04-25 04:45:49 UTC
Permalink
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.

Paul
Vaibhav.bhawsar
2008-04-25 02:50:10 UTC
Permalink
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.

<code>
import MySQLdb
import sys
# connect to the MySQL server
try:
conn =
MySQLdb.connect(host="localhost",read_default_file='~/.my.cnf',db='test',cursorclass=MySQLdb.cursors.DictCursor)
cur = conn.cursor()
print conn
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
</code>

Traceback (most recent call last):
File "/Users/.....src/db/test.py", line 7, in ?
conn =
MySQLdb.connect(host="localhost",read_default_file='~/.my.cnf',db='te
st',cursorclass=MySQLdb.cursors.DictCursor)
AttributeError: 'module' object has no attribute 'cursors'
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080424/79d9445a/attachment.html>
Vaibhav.bhawsar
2008-04-25 06:58:53 UTC
Permalink
Great both methods worked! I dont quite understand this since i imported the
whole module with "import MySQLdb"

Thanks!
Post by Paul McNett
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.
"""
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.
Paul
--
Vaibhav Bhawsar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080425/fc4f3465/attachment.html>
Steve Holden
2008-04-25 11:38:08 UTC
Permalink
Vaibhav.bhawsar wrote:
[top-posting amended: see below]
On Fri, Apr 25, 2008 at 12:45 AM, Paul McNett <p at ulmcnett.com
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.
"""
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/
Vaibhav.bhawsar
2008-04-25 17:16:11 UTC
Permalink
Hmm that explains it! Thank you.
v
Post by Steve Holden
[top-posting amended: see below]
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.
"""
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/
--
http://mail.python.org/mailman/listinfo/python-list
--
Vaibhav Bhawsar
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080425/33d7fc18/attachment.html>
Loading...