waliwali / ibm-db

Automatically exported from code.google.com/p/ibm-db
0 stars 0 forks source link

Fetch for decimal returns string #129

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Fetch for a decimal column always returns a string, although the type of the 
column is 'decimal' .. a small program follows

from ibm_db import *
import decimal
con=connect("DRIVER={IBM DB2 ODBC DRIVER};PROTOCOL=TCPIP;"+
  "DATABASE=...;HOSTNAME=localhost;PORT=50000;UID=...;PWD=...",
   '','',{},QUOTED_LITERAL_REPLACEMENT_OFF)

try: exec_immediate(con,'drop table xdec')
except: pass

exec_immediate(con,
   'create table xdec (decn dec(7) not null with default)')
exec_immediate(con,'insert into xdec values(123456)')

rs = exec_immediate(con,"select decn from xdec")
row = fetch_tuple(rs)

print 'Name=',field_name(rs,0), 'Type=',field_type(rs,0),'Value=', row[0]
print 
x=row[0]+decimal.Decimal(10)

This is the output
-----------------------------------------------------------------------------
Name= DECN Type= decimal Value= 123456

Traceback (most recent call last):
  File "E:\P\PY\sql\T.PY", line 19, in <module>
    x=row[0]+decimal.Decimal(10)
TypeError: cannot concatenate 'str' and 'Decimal' objects

What version of the product are you using? On what operating system?
ibm_db: 2.0.2
db2: 10.1 fp 2 (db2 connect)
op sys: windows 7 

Original issue reported on code.google.com by zisisk...@gmail.com on 7 May 2013 at 12:57

GoogleCodeExporter commented 9 years ago
This is the current limitation of ibm_db driver due to lack of C API of 
python's Decimal object, but you will get decimal object through ibm_db_dbi 
wrapper.

eg.
 import ibm_db, ibm_db_dbi
 conn = ibm_db.connect('conn_str', '', '')
 dbi_conn = ibm_db_dbi.Connection(conn)
 cur = dbi_conn.cursor()
 cur.execute('select statement')
 row = cur.fetchone()

Original comment by rahul.pr...@in.ibm.com on 8 May 2013 at 12:09

GoogleCodeExporter commented 9 years ago
OK thanks

Original comment by zisisk...@gmail.com on 8 May 2013 at 12:27

GoogleCodeExporter commented 9 years ago

Original comment by rahul.pr...@in.ibm.com on 17 Jun 2013 at 6:26