python - cx_Oracle: How can I receive each row as a dictionary? -
by default, cx_oracle returns each row tuple.
>>> import cx_oracle >>> conn=cx_oracle.connect('scott/tiger') >>> curs=conn.cursor() >>> curs.execute("select * foo"); >>> curs.fetchone() (33, 'blue') how can return each row dictionary?
you can override cursor's rowfactory method. need each time perform query.
here's results of standard query, tuple.
curs.execute('select * foo') curs.fetchone() (33, 'blue') returning dictionary:
def makenamedtuplefactory(cursor): columnnames = [d[0].lower() d in cursor.description] import collections row = collections.namedtuple('row', columnnames) return row curs.rowfactory = makedictfactory(curs) curs.fetchone() {'y': 'brown', 'x': 1} returning named tuple:
def makedictfactory(cursor): columnnames = [d[0] d in cursor.description] def createrow(*args): return dict(zip(columnnames, args)) return createrow curs.rowfactory = makenamedtuplefactory(curs) curs.fetchone() row(x=33, y='blue') credit amaury forgeot d'arc: http://sourceforge.net/p/cx-oracle/mailman/message/27145597
Comments
Post a Comment