Thread: [cx-oracle-users] Tuple of Dictionaries query results
Brought to you by:
atuining
From: Tom H. <tom...@sc...> - 2006-04-18 15:25:23
|
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 |
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 > |
From: <wa...@li...> - 2006-04-18 15:42:03
|
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()) Or you could use ll-orasql (http://www.livinglogic.de/Python/orasql/) which does exactly that (and makes the fields available as attributes too). Bye, Walter Dörwald |
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 |
From: Hancock, D. \(DHANCOCK\) <DHA...@ar...> - 2006-04-18 15:42:16
|
I think that the module dtuple.py (written by Greg Stein) will do what = you want (it's very flexible in how you can refer to the query results). = Actually, I'm not 100% sure that you'll get the tuple of dictionaries, = but that would be a very simple wrapper. =20 http://www.lyra.org/greg/python/dtuple.py =20 Cheers! -- David Hancock ________________________________ From: cx-...@li... on behalf of Tom = Haddon Sent: Tue 4/18/2006 11:23 AM To: cx-...@li... Subject: [cx-oracle-users] Tuple of Dictionaries query results 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 = 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=3Dlnk&kid=3D110944&bid=3D241720&dat=3D= 121642 _______________________________________________ cx-oracle-users mailing list cx-...@li... https://lists.sourceforge.net/lists/listinfo/cx-oracle-users |