python-cx_Oracle icon indicating copy to clipboard operation
python-cx_Oracle copied to clipboard

Fail to make queries return empty strings instead of NULLs

Open bigfoxtail opened this issue 5 years ago • 2 comments

use https://cx-oracle.readthedocs.io/en/latest/user_guide/sql_execution.html#changing-query-results-with-outconverters code

def OutConverter(value):
    if value is None:
        return ''
    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, cur.arraysize, outconverter=OutConverter)

connection.outputtypehandler = OutputTypeHandler

But the query will still return a result of none.Is it my wrong way to use it or is it a bug? thank you

bigfoxtail avatar Jun 20 '20 03:06 bigfoxtail

I took a look and realized that the example provided in the documentation is inaccurate! We will have to adjust it and also make clear that the output type handler only handles values that are not null (or the value None). So right now there is no way to make queries return the empty string instead of None. I'll consider a way to deal with that in the future..

anthony-tuininga avatar Jun 25 '20 04:06 anthony-tuininga

One possible solution: add a convert_nulls parameter to cursor.var() which has a default value of False. When True, however, None would be passed through to the outconverter and inconverter functions. This retains the current behavior but gives the option of greater control for those (like you) who would prefer that. Thoughts?

anthony-tuininga avatar Sep 15 '20 19:09 anthony-tuininga