Re: [cx-oracle-users] Help with fetchraw
Brought to you by:
atuining
From: Chris D. <cdu...@ya...> - 2005-05-17 09:15:14
|
See in-line --- Anthony Tuininga <ant...@gm...> wrote: > On 5/15/05, Leith Parkin <lei...@gm...> wrote: > > Hi Chris, > > > > This is a quick example i knocked togethor ages ago to return a dict > > result set instead of tuples. In retrospect fetchall() would probably > > be better writtern as a generator under the newer python stuff. > > Definitely. :-) Especially if you wanted to use it for large result sets. > > > eg r = db.fetchone() > > r['FIELDNAME'] > > > > Prehaps Anthony could give some feedback as to the "correctness" of this. > > Looks "correct" to me. It works, doesn't it?? :-) As suggested, if you > wanted to override the __iter__() method (or the fetchall() as shown > below) to replace None with "" for null strings, that would be quite > doable. If you need an example of that, I could probably whip > something up. As you can see by this example, subclassing to extend or > override functionality is actually quite simple. Anthony, If you could supply a simple example that would be much appreciated. Thanks, Chris > > > --- SNIP --- > > from cx_Oracle import * > > > > # DictCursor for Oracle, returns dicts instead of tuples > > class DictConnect(Connection): > > def cursor(self): > > return DictCursor(self) > > > > class DictCursor(Cursor): > > def fetchall(self): > > r = Cursor.fetchall(self) > > result = [] > > for l in r: > > result.append( dict([(a[0],b) for a,b in > > zip(self.description, l)]) ) > > > > return result > > > > def fetchone(self): > > r = Cursor.fetchone(self) > > if r: > > return dict([(a[0],b) for a,b in zip(self.description, r)]) > > else: > > return None > > > > # factory class > > def connect(**kw): > > return DictConnect(**kw) > > > > ---- SNIP ---- > > > > On 5/15/05, Chris Dunscombe <cdu...@ya...> wrote: > > > Leith, > > > > > > Thanks, I like the sub-classing idea but my OO Python isn't that good so if you could give > me a > > > code example that would be much appreciated. > > > > > > Thanks very much, > > > > > > Chris > > > --- Leith Parkin <lei...@gm...> wrote: > > > > > > > Hi Chris, > > > > > > > > No worries. We have a very similair setup at work, transferring > > > > summarised data from oracle into MySQL. > > > > > > > > Depending on the database you are transferring too, couldnt you set a > > > > trigger, inserting NULL (eg, a python None from Oracle) into the table > > > > generates an empty string? > > > > > > > > Failing that, subclassing cx_Oracles cursor object to return a the > > > > empty string when the column type is textual and the value is null > > > > might be another option. > > > > > > > > Good luck, > > > > > > > > Leith > > > > __________________________________ Do you Yahoo!? Make Yahoo! your home page http://www.yahoo.com/r/hs |