从Python ORA-01790修复oracledb中的数据类型错误

disbfnqx  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(164)

我有以下代码:

data = [1,2,None,4,5,6]
sql = """
SELECT :1 AS ID, :2 AS pkey, :3 AS issuenum FROM dual  UNION ALL
SELECT :4 AS ID, :5 AS pkey, :6 AS issuenum FROM dual 
"""

cur = con.cursor()
cur.execute(sql, data)
data = cur.fetchall()

字符串
我生成错误DatabaseError: ORA-01790: expression must have same datatype as corresponding expression
如果我用data = [1,2,3,4,5,6]运行它,一切都通过了。
我的问题是,如果列表中的值为None,那么我应该如何对语句进行出价,才能选择NULL值?

6tqwzwtp

6tqwzwtp1#

这是一个SQL问题,与驱动程序无关。您可以通过发出以下语句来修复此问题:

SELECT :1 AS ID, :2 AS pkey, to_number(:3) AS issuenum FROM dual
UNION ALL
SELECT :4 AS ID, :5 AS pkey, to_number(:6) AS issuenum FROM dual

字符串
这将确保两行的数据类型都是number。在Python中,值None没有类型,因此驱动程序假设值是字符串。
您也可以在驱动程序中解决此问题,方法是在执行SQL之前使用以下命令:

cur.setinputsizes(
    oracledb.DB_TYPE_NUMBER,
    oracledb.DB_TYPE_NUMBER,
    oracledb.DB_TYPE_NUMBER,
    oracledb.DB_TYPE_NUMBER,
    oracledb.DB_TYPE_NUMBER,
    oracledb.DB_TYPE_NUMBER,
)


这将强制驱动程序将所有绑定值视为number,包括值None

相关问题