简体   繁体   English

使用cx_Oracle从Oracle提取数据时,返回空字符串代替Oracle NULL / Python无

[英]Return Empty String in place of Oracle NULL/Python None when pulling data from Oracle using cx_Oracle

When I pull data using the cx_Oracle module, None is returned in place of Oracle NULL. 当我使用cx_Oracle模块提取数据时,返回None代替Oracle NULL。 At this point I'm using the below function to convert None to empty string. 此时我正在使用以下函数将None转换为空字符串。

def xstr(s):
    if s is None:
        return ''
    return str(s)

Is there a way to make the cx_Oracle module to return an empty string instead of None? 有没有办法让cx_Oracle模块返回空字符串而不是None?

You can use an output type handler and out converter to transform this way. 您可以使用输出类型处理程序和输出转换器来进行转换。 Something like this. 像这样的东西。

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, cursor.arraysize, outconverter=OutConverter)

connection.outputtypehandler = OutputTypeHandler

The above code will transform any null strings that are returned from the database to empty strings. 上面的代码会将从数据库返回的任何空字符串转换为空字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM