在用户定义类型上将 null 从 python 传递到 oracle 存储过程

Passing null to an oracle stored proc from python on a user defined type

我正在尝试使用来自 python 的 Oracle 存储过程,但遇到了用户定义类型的问题。

这是重现问题的最小示例

create or replace
TYPE  test_type AS TABLE OF VARCHAR2(4000);
/

create or replace procedure python_test (
 a in number,
 b in test_type) as
begin
  null;
end;
/

我实际上不希望从 python 传递任何东西作为 test_type 但只是不将其设置为任何东西(这是现实生活中存储过程所允许的)。

在SQL中,以下作品:

execute python_test(1,null);

但是,从 python 到 cx_Oracle,以下命令

cursor.callproc('python_test', [1, None])

给我以下错误:

PLS-00306: wrong number or types of arguments in call to 'PYTHON_TEST'

有没有办法让它工作?

是的。您需要使用 cursor.setinputsizes() 才能使这项工作按预期进行。如果你简单地使用默认处理 None 被视为一个字符串,这不是这里所期望的。相反,执行如下操作:

import cx_Oracle

conn = cx_Oracle.Connection("cx_Oracle/welcome@oracle-laptop/T122")
cursor = conn.cursor()

var = cursor.var(cx_Oracle.OBJECT, typename = "TEST_TYPE")
cursor.setinputsizes(None, var)
cursor.callproc("python_test", [1, None])