使用 cx_Oracle SQL/XML 查询将 Java 转换为 Python

Convert Java to Python using cx_Oracle SQL/XML query

我在 Java 中有一个现有的解决方案可以从 Oracle 数据库中提取数据。我是这样做的:

String tmp = "begin ? := pkgioexportora.request(?); end;";
String xml = "<ttc.export.public.data.search><query><popid>1</popid> <moduleid>3</moduleid><only_changes>0</only_changes></query></ttc.export.public.data.search>";
CallableStatement callableStmt = oracle.prepareCall(tmp); 
// Register the type of the out param - an Oracle specific type
callableStmt.registerOutParameter(1, OracleTypes.NUMBER);
callableStmt.setString(2, xml);

此构造 returns 一个作业 ID,我稍后需要在 SELECT 查询的 WHERE 子句中使用它。

我试过只使用游标并输入完整的语句,没有 CallableStatement 的东西,但运气不好。

cursor = con.cursor()
cursor.execute("begin 2 := pkgioexportora.request(xml_stuff_here); end;";)

callproc 似乎给我类似的错误。

我已经尝试搜索解决方案或完成类似的事情,但尚未提出任何示例。是否可以用 cx_Oracle 做这样的事情,或者我是否坚持使用我的 Java 代码来做这件事?

您可以在 cx_Oracle 中使用 cursor.callfunc()。如下:

result = cursor.callfunc("pkgioexportora.request", cx_Oracle.NUMBER, [xml_stuff])

你也可以这样做,但是比较复杂:

var = cursor.var(cx_Oracle.NUMBER) cursor.execute("begin :1 := pkgioexportora.request(:2); end;", [var, xml_stuff]) result = var.getvalue()