[DB-SIG] reg cursor.fetchall()

Andy Dustman farcepest at gmail.com
Fri Feb 10 22:52:24 CET 2006

On 2/8/06, Andy Todd <andy47 at halfcooked.com> wrote:

> Sadly cx_Oracle doesn't offer different cursor classes like, for
> instance, MySQLdb;
> http://dustman.net/andy/python/MySQLdb_obsolete/doc/MySQLdb-4.html#usage
> So you can't refer to cursor results as dictionaries or other sequence
> types.

You can actually take any DB-API database result row and turn it into
a dictionary:

>>> import MySQLdb
>>> db=MySQLdb.connect(db="mysql",read_default_file="~/.my.cnf")
>>> c=db.cursor()
>>> c.execute("select * from user")
>>> for row in c.fetchall():
...     d = dict( [ (c.description[i][0], j) for i,j in enumerate(row) ] )
...     print d
{'Drop_priv': 'Y', 'Execute_priv': 'Y', 'Create_routine_priv': 'Y',
'Repl_client_priv': 'Y', 'Create_user_priv': 'Y', 'Create_priv': 'Y',
'References_priv': 'Y', 'max_user_connections': 0L, 'Shutdown_priv':
'Y', 'Grant_priv': 'Y', 'max_updates': 0L, 'max_connections': 0L,
'Show_db_priv': 'Y', 'Reload_priv': 'Y', 'Super_priv': 'Y', 'Host':
'localhost', 'User': 'root', 'Alter_priv': 'Y', 'ssl_cipher':
array('c'), 'Password': 'xxx', 'Delete_priv': 'Y', 'Repl_slave_priv':
'Y', 'Insert_priv': 'Y', 'x509_subject': array('c'), 'ssl_type': '',
'Index_priv': 'Y', 'Create_tmp_table_priv': 'Y', 'x509_issuer':
array('c'), 'Create_view_priv': 'Y', 'Select_priv': 'Y',
'Show_view_priv': 'Y', 'Update_priv': 'Y', 'Lock_tables_priv': 'Y',
'Process_priv': 'Y', 'Alter_routine_priv': 'Y', 'File_priv': 'Y',
'max_questions': 0L}

Or make a helper function to do this. (Example left as an exercise for
the reader.)

The Pythonic Principle: Python works the way it does
because if it didn't, it wouldn't be Python.

More information about the DB-SIG mailing list