Re: [cx-oracle-users] Tuple of Dictionaries query results
Brought to you by:
atuining
From: Tom H. <tom...@sc...> - 2006-04-18 15:59:24
|
Hi Anthony, Thanks for the tip. And thanks to others for their feedback as well. I went with this idea as it suited me best in this case, but the other comments gave be ideas for other approaches as well. Thanks, Tom On Tue, 2006-04-18 at 09:35 -0600, Anthony Tuininga wrote: > 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 = 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 = DictCursor > > > > ... > > > > curs = conn.cursor(cursortype) > > result = curs.execute("%s" % (sql, )) > > return curs.fetchall() > > > > ------------------- > > > > And in Postgres I do it like this: > > > > ----------------- > > > > import pg > > > > ... > > > > result = 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 language > > that extends applications into web and mobile media. Attend the live webcast > > and join the prime developer group breaking into this new coding territory! > > http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642 > > _______________________________________________ > > cx-oracle-users mailing list > > cx-...@li... > > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > > > > ------------------------------------------------------- > This SF.Net email is sponsored by xPML, a groundbreaking scripting language > that extends applications into web and mobile media. Attend the live webcast > and join the prime developer group breaking into this new coding territory! > http://sel.as-us.falkag.net/sel?cmd=lnk&kid0944&bid$1720&dat1642 > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users -- Tom Haddon Systems Engineer 198 High Holborn London, WC1V 7BD T +44 (0) 20 7959 0630 www.scansafe.com ScanSafe - Pure Internet |