[cx-oracle-users] looking for a quicker / more elegant way to form objects with attributes out of q
Brought to you by:
atuining
From: Massa, H. A. <ch...@gh...> - 2011-03-03 11:25:36
|
Hello, I am using cx_Oracle to provide Oracle-Acces for a PostgreSQL database via pl/python. To be able to return an cx_Oracle result set as a "set of records" from a function (which is similiar to the "table returning functions" within Oracle), the columns of the record need to be attributes of an object. Actually I do: ------------------- import cx_Oracle cn=cx_Oracle.connect("user","password","tnsname") cs=cn.cursor() cs.execute("""select id, firstname, secondname, street, zipcode from adresses""") daten=cs.fetchall() cn.commit() class xrow(object): pass ergebnis=[] fieldnames = [a[0].lower() for a in cs.description] for row in daten: myrow=xrow() for index, fn in enumerate(fieldnames): setattr(myrow,fn,row[index]) ergebnis.append(myrow) return ergebnis which gives me a nice table as a result of that function call, which can be perfectly used. Now... that "loop through the result and attach every column to a stub object" does not feel very elegant; and it needs python to loop through every field. Is there a more elegant / faster / more compact solution? Best wishes, Harald -- GHUM GmbH Harald Armin Massa Spielberger Straße 49 70435 Stuttgart 0173/9409607 Amtsgericht Stuttgart, HRB 734971 - persuadere. et programmare |