outconverter doesn't work when the value is Null · Issue #640 · oracle/python-cx_Oracle (original) (raw)
- What versions are you using?
platform.platform: Windows-10-10.0.19045-SP0
sys.maxsize > 2**32: True
platform.python_version: 3.8.10 - My database version
cx_Oracle.version: 8.3.0
cx_Oracle.clientversion: (12, 1, 0, 2, 0) - Describe the problem
I have a requirement to get data from the database through cx_oracle convert. and during the fetch data, if the value of the Number field is None, it needs to be converted to -1.
I want to use outconverter attribute of the Variable. but I found if the value is None, the outconverter isn't fired.
- Include a runnable Python script that shows the problem.
import cx_Oracle
with cx_Oracle.connect("omrscpif" ,"omrscpif", 'ammiceng') as connect: def OutConverter(value): if value is None: return '' try: return value except: return ''
def NumberOutConverter(value):
if value is None:
return -1
return value
def OutputTypeHandler(cursor, name, defaultType, size, precision, scale):
if defaultType in (cx_Oracle.STRING, cx_Oracle.FIXED_CHAR):
return cursor.var(str, size, cursor.arraysize, outconverter=OutConverter)
if defaultType == cx_Oracle.DB_TYPE_NUMBER:
return cursor.var(cx_Oracle.DB_TYPE_NUMBER, size, cursor.arraysize, outconverter=NumberOutConverter)
# Finally, we will assign the defination to the outputtypehandler of the connect.
connect.outputtypehandler = OutputTypeHandler
with connect.cursor() as cursor:
cursor.execute("select fordertypevalue, forderqtyfuturecommitted, fbackorderqty from so_send where ftrnflg = 'N'")
result = cursor.fetchall()
print(result)
- the result is below