0

It appears as if decimal values are treated as strings by ibm_db. Small example:

Python 2.7.14 (default, Mar 14 2018, 13:36:31) [GCC 7.3.1 20180303 (Red Hat 7.3.1-5)] on linux2

]$ cat aa.py 
#!/usr/bin/python

import sys
import ibm_db

conn = ibm_db.connect("dsn=sample","********","********")
s1 = ibm_db.prepare(conn, "values (cast(1 as int),cast(0.1 as decimal(2,1)))")
ibm_db.execute(s1)
d1 = ibm_db.fetch_both(s1)
print d1[0], type(d1[0]), d1[1], type(d1[1])

Output:

1 <type 'long'> 0.1 <type 'str'>

I'm dumping the dict as JSON to a file (for filebeat -> logstash), so I really need numeric types to stay numeric. I guess I can check the prepared statement for the type (the type is correct there) and massage the data before dumping, but that seems terribly wrong.

Any thoughts on how to get numeric data into the JSON object in the simplest possible way?

I found the following 3 year old mention:

https://github.com/ibmdb/python-ibmdb/issues/129

is this limitation still in play?

0

I'm not sure if it's supposed to work or not, in

https://github.com/ibmdb/python-ibmdb/issues/129

it says:

"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."

I used the following workaround:

import ibm_db
import ibm_db_dbi
[...]
ibm_db_conn = ibm_db.connect("DATABASE=%s;HOSTNAME=%s;PORT=%d;PROTOCOL=TCPIP;UID=%s; PWD=%s" % (dbname, host, port, user, passwd),"","")
conn = ibm_db_dbi.Connection(ibm_db_conn)
c1 = conn.cursor()
c1.execute("values (cast(1 as int),cast(0.1 as decimal(2,1)))")
row = c1.fetchone()
if row == None:
    sys.exit(1)

# Manually create a dictionary
rowDict = dict(zip([c[0] for c in c1.description], row))
print rowDict
{'1': 1, '2': Decimal('0.1')}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.