Re: [cx-oracle-users] More than one type returned for a single column
Brought to you by:
atuining
From: Anssi K. <aka...@gm...> - 2012-09-30 15:54:42
|
Same problem when using a table, too: import cx_Oracle connection = cx_Oracle.connect('user/passwd@' + cx_Oracle.makedsn('localhost', 1521, 'xe')) cur = connection.cursor() cur.execute("create table testt(col1 number, col2 number(5, 2))") cur.execute("insert into testt values(1, 1)") cur.execute("insert into testt values(1.1, 1.1)") cur.execute("select col1, col2 from testt") rows = list(cur.fetchall()) col1_vals = [row[0] for row in rows] col2_vals = [row[1] for row in rows] print col1_vals print col2_vals print set([type(v) for v in col1_vals]) print set([type(v) for v in col2_vals]) OUT: [1, 1.1] [1.0, 1.1] set([<type 'float'>, <type 'int'>]) set([<type 'float'>]) Describe of the table tells the type of col1 is plain NUMBER. - Anssi On Sun, Sep 30, 2012 at 5:51 PM, Dwayne King <dw...@kr...>wrote: > It *is* optional in oracle to supply the precision and scale when > declaring a data type (in which case it will use the maximums for the > type), so perhaps this is what happens. > > Can you do a describe on the table in question from sqlplus and show us? > > Create a table like: > Create table test( col1 number, col2 number(5,2)) > > Then see if the first column gives you back what you're seeing. Sorry - > not close to a computer right now > > Sent from my iPad > > On 2012-09-29, at 8:42 PM, Anssi Kääriäinen <aka...@gm...> wrote: > > > On Sun, Sep 30, 2012 at 3:12 AM, Chris Gould <chr...@to...>wrote: > >> Arent they really both type NUMBER ? >> >> > It have an (incorrect?) expectation that one column should have one result > type in Python. While float and int are close to each other in Python, they > do have differences, for example the '/' operator. > > The situation came up in trying to use outputtypefactory in Django, then > optimizing the case where the type of the result is guessed, see > https://github.com/shaib/django/blob/d10eaf28c864816e9bb8b0d1467e90b79291792a/django/db/backends/oracle/base.py#L772- the problem there is that half of the numbers are guessed as Decimal, > half as int. This isn't a nice situation, the APIs for Decimal and int are > very different. > > I don't know Oracle too well - is the situation such that the result is > NUMBER in Oracle, and there is no more information about the column > available from the DB? > > - Anssi > > > >> >> On Sunday, September 30, 2012, Anssi Kääriäinen wrote: >> >>> It seems it is possible to have more than one type returned for a single >>> column. Observe this test case: >>> >>> import cx_Oracle >>> print cx_Oracle.version >>> connection = cx_Oracle.connect('user/ >>> passwd@' + cx_Oracle.makedsn('localhost', 1521, 'xe')) >>> cur = connection.cursor() >>> cur.execute("select case when dbms_random.random > 0.5 then 0.1 else 0 >>> end from testtable") >>> vals = [] >>> vals.extend(row[0] for row in cur.fetchall()) >>> print set([type(v) for v in vals]) >>> >>> The output is: >>> 5.1.2 >>> set([<type 'float'>, <type 'int'>]) >>> >>> To me this seems surprising - shouldn't a column have the same type >>> across all rows in a single resultset? >>> >>> - Anssi >>> >> >> >> -- >> Sent from Gmail Mobile >> >> >> ------------------------------------------------------------------------------ >> Everyone hates slow websites. So do we. >> Make your web apps faster with AppDynamics >> Download AppDynamics Lite for free today: >> http://ad.doubleclick.net/clk;258768047;13503038;j? >> http://info.appdynamics.com/FreeJavaPerformanceDownload.html >> _______________________________________________ >> cx-oracle-users mailing list >> cx-...@li... >> https://lists.sourceforge.net/lists/listinfo/cx-oracle-users >> >> > > ------------------------------------------------------------------------------ > Everyone hates slow websites. So do we. > Make your web apps faster with AppDynamics > Download AppDynamics Lite for free today: > http://ad.doubleclick.net/clk;258768047;13503038;j? > http://info.appdynamics.com/FreeJavaPerformanceDownload.html > > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > > > ------------------------------------------------------------------------------ > Everyone hates slow websites. So do we. > Make your web apps faster with AppDynamics > Download AppDynamics Lite for free today: > http://ad.doubleclick.net/clk;258768047;13503038;j? > http://info.appdynamics.com/FreeJavaPerformanceDownload.html > _______________________________________________ > cx-oracle-users mailing list > cx-...@li... > https://lists.sourceforge.net/lists/listinfo/cx-oracle-users > > |