Databases: Getting values by column name

Robert Ferber rob at nospam.net
Tue Aug 17 15:17:17 EDT 2004


Skip Montanaro wrote:

> Yes, for MySQLdb import the MySQLdb.cursors module then set the
> cursorclass arg when you create a connection, like so:
> 
>     import MySQLdb
>     import MySQLdb.cursors
>     ...
>     conn = MySQLdb.Connection(host=...,
>                               user=...,
>                               passwd=...,
>                               db=...,
>                               cursorclass=MySQLdb.cursors.DictCursorNW,
>                               ...)

You mean MySQLdb.connect()?

> I think the reason this sort of behavior isn't spelled out in PEP 249 is
> because it varies too much from one database to another.  Psycopg (a
> PostgreSQL adaptor) does it differently.  Instead of specifying the cursor
> type when the connection is created, you instantiate a different cursor
> class:
> 
>     conn = psycopg.connect("...")

NameError: name 'psycopg' is not defined

Also wouldn't that overwrite the above conn variable?

>     ...
>     cursor = conn.dictcursor()

 
AttributeError: Connection instance has no attribute 'dictcursor' 

(When I use the conn from MySQLdb.connect)

> I imagine there are some databases that either don't support name-based
> retrieval very well (or at all), or make it difficult to get at.  You
> might get a more solid response on the reasons for this omission from PEP
> 249 from the db-sig folks:
> 
>     http://www.python.org/sigs/db-sig/

Isn't there any documentation with a simple example out there?

I already know the stuff on that page, it's really slowing you down :-(

Robert





More information about the Python-list mailing list