Re: [cx-oracle-users] Tuple of Dictionaries query results
Brought to you by:
atuining
From: Anthony T. <ant...@gm...> - 2006-04-18 15:35:41
|
If you want to do this you should be able to do it as follows. Note that this will reduce performance quite dramatically but if you are only dealing with a handful of rows the additional clarity might be worthwhile. class Cursor(cx_Oracle.Cursor): def __TransformToDict(self, row): return dict([(info[0], value) for info, value in zip(cursor.description, row)]) def fetchone(self): row =3D super(Cursor, self).fetchone() return self.__TransformToDict(row) etc. (for fetchall() and fetchmany()) Note that this code has not actually been tested but it should give you an idea of how to go about doing this if you have a mind to do so. :-) On 4/18/06, Tom Haddon <tom...@sc...> wrote: > Hi Folks, > > I was wondering if anyone knows if there's a way to grab the results of > an Oracle query into a tuple of dictionaries (one for each row with the > key as the column name and the value as the value of the column)? > > In mysql I do this as below: > > ---------------- > > import MySQLdb > from MySQLdb.cursors import DictCursor > cursortype =3D DictCursor > > ... > > curs =3D conn.cursor(cursortype) > result =3D curs.execute("%s" % (sql, )) > return curs.fetchall() > > ------------------- > > And in Postgres I do it like this: > > ----------------- > > import pg > > ... > > result =3D conn.query("%s" % (sql, )) > return result.dictresult() > > ------------------ > > I can't seem to see if there are any cursor types as with MySQLdb > module, or a dictresult query return method as with the pg module. > > Basically this then allows me to reference the data as follows: > > for row in TupleOfDicts(): > print "%s %s" % (row['column1'], row['column2']) > > Any help appreciated. > > Thanks, Tom > -- > Tom Haddon > Systems Engineer > > 198 High Holborn > London, WC1V 7BD > > T +44 (0) 20 7959 0630 > www.scansafe.com > > ScanSafe - Pure Internet > > > ------------------------------------------------------- > This SF.Net email is sponsored by xPML, a groundbreaking scripting langua= ge > that extends applications into web and mobile media. Attend the live webc= ast > and join the prime developer group breaking into this new coding territor= y! > http://sel.as-us.falkag.net/sel?cmd=3Dlnk&kid=3D110944&bid=3D241720&dat= =3D121642 > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > |