Re: [cx-oracle-users] Help with fetchraw
Brought to you by:
atuining
From: Anthony T. <ant...@gm...> - 2005-05-16 23:02:22
|
On 5/15/05, Leith Parkin <lei...@gm...> wrote: > Hi Chris, >=20 > 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 =3D db.fetchone() > r['FIELDNAME'] >=20 > 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. > --- SNIP --- > from cx_Oracle import * >=20 > # DictCursor for Oracle, returns dicts instead of tuples > class DictConnect(Connection): > def cursor(self): > return DictCursor(self) >=20 > class DictCursor(Cursor): > def fetchall(self): > r =3D Cursor.fetchall(self) > result =3D [] > for l in r: > result.append( dict([(a[0],b) for a,b in > zip(self.description, l)]) ) >=20 > return result >=20 > def fetchone(self): > r =3D Cursor.fetchone(self) > if r: > return dict([(a[0],b) for a,b in zip(self.description, r)= ]) > else: > return None >=20 > # factory class > def connect(**kw): > return DictConnect(**kw) >=20 > ---- SNIP ---- >=20 > 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 s= o 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 tabl= e > > > 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 > > > > > > > > > On 5/13/05, Chris Dunscombe <cdu...@ya...> wrote: > > > > Leith, > > > > > > > > I didn't mention that for 1) the target table is in a separate data= base and using database > > > links > > > > isn't an option (although I wish it was) due to reasons that are to= o lengthy to go into here. > > > > Hence PL/SQL isn't really viable. So I'd still like to look at cur= sor.fetchraw() > > > > > > > > Now for 2) you maybe right that PL/SQL could be used but I'll still= have to write out to file > > > > using Python as util_file can only write to files that the database= server can "see" which > > > isn't > > > > always the case for my app. > > > > > > > > Thanks for the idea, > > > > > > > > Chris > > > > > > > > > > > > --- Leith Parkin <lei...@gm...> wrote: > > > > > This sounds like something better done in PL/SQL? > > > > > > > > > > Regards, > > > > > > > > > > Leith > > > > > On 5/13/05, Chris Dunscombe <cdu...@ya...> wrote: > > > > > > I'm looking to use cursor.fetchraw to help improve performance = in an app I'm writing. I've > > > > > looked > > > > > > at CopyData from cx_OracleTools but I'm afraid I couldn't work = it out. > > > > > > If possible I'd like a simple full example to show me the way. = The context is: > > > > > > > > > > > > 1) Retrieve rows from source table and insert into a target tab= le which has exactly the > > > same > > > > > > structure. > > > > > > > > > > > > 2) Retrieve rows from source table, add some column formatting = and write out to a file. > > > > > > > > > > > > Thanks very much, > > > > > > > > > > > > Chris Dunscombe > > > > > > > > > > > > PS Thanks for all the input re my previous posting on Nulls and= None. I've still got some > > > > > thinking > > > > > > and work to do on which is best for performance and how best to= cope with different > > > datatypes, > > > > > > particularly Longs (what a pain they are!!) > > > > > > > > > > > > __________________________________ > > > > > > Yahoo! Mail Mobile > > > > > > Take Yahoo! Mail with you! Check email on your mobile phone. > > > > > > http://mobile.yahoo.com/learn/mail > > > > > > > > > > > > ------------------------------------------------------- > > > > > > This SF.Net email is sponsored by Oracle Space Sweepstakes > > > > > > Want to be the first software developer in space? > > > > > > Enter now for the Oracle Space Sweepstakes! > > > > > > http://ads.osdn.com/?ad_id=3D7393&alloc_id=3D16281&op=3Dclick > > > > > > _______________________________________________ > > > > > > cx-oracle-users mailing list > > > > > > cx-...@li... > > > > > > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > > > > > > > > > > > > > > > > > > > > ------------------------------------------------------- > > > > > This SF.Net email is sponsored by Oracle Space Sweepstakes > > > > > Want to be the first software developer in space? > > > > > Enter now for the Oracle Space Sweepstakes! > > > > > http://ads.osdn.com/?ad_ids93&alloc_id=16281&op=3Dclick > > > > > _______________________________________________ > > > > > cx-oracle-users mailing list > > > > > cx-...@li... > > > > > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > > > > > > > > > > > > __________________________________________________ > > > > Do You Yahoo!? > > > > Tired of spam? Yahoo! Mail has the best spam protection around > > > > http://mail.yahoo.com > > > > > > > > > > > > ------------------------------------------------------- > > > > This SF.Net email is sponsored by Oracle Space Sweepstakes > > > > Want to be the first software developer in space? > > > > Enter now for the Oracle Space Sweepstakes! > > > > http://ads.osdn.com/?ad_id=3D7393&alloc_id=3D16281&op=3Dclick > > > > _______________________________________________ > > > > cx-oracle-users mailing list > > > > cx-...@li... > > > > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > > > > > > > > > > > > ------------------------------------------------------- > > > This SF.Net email is sponsored by Oracle Space Sweepstakes > > > Want to be the first software developer in space? > > > Enter now for the Oracle Space Sweepstakes! > > > http://ads.osdn.com/?ad_ids93&alloc_id=16281&op=3Dclick > > > _______________________________________________ > > > cx-oracle-users mailing list > > > cx-...@li... > > > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > > > > > > __________________________________________________ > > Do You Yahoo!? > > Tired of spam? Yahoo! Mail has the best spam protection around > > http://mail.yahoo.com > > > > ------------------------------------------------------- > > This SF.Net email is sponsored by Oracle Space Sweepstakes > > Want to be the first software developer in space? > > Enter now for the Oracle Space Sweepstakes! > > http://ads.osdn.com/?ad_id=3D7393&alloc_id=3D16281&op=3Dclick > > _______________________________________________ > > cx-oracle-users mailing list > > cx-...@li... > > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > >=20 > ------------------------------------------------------- > This SF.Net email is sponsored by Oracle Space Sweepstakes > Want to be the first software developer in space? > Enter now for the Oracle Space Sweepstakes! > http://ads.osdn.com/?ad_ids93&alloc_id=16281&opclick > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > |