[英]Calling a stored function from oracle DB with cx_Oracle in Python
I am not familiar with PL/SQL and just can't transform a working raw query into python code.我不熟悉 PL/SQL,只是无法将工作的原始查询转换为 python 代码。
Here is the raw query:这是原始查询:
DECLARE
returntype MY_PKG_1.MY_DATA_TYPE;
BEGIN
SELECT *
BULK COLLECT INTO returntype
FROM TABLE(MY_PKG_2.MY_FUNC(
param_1 => 'some data',
param_2 => 'some more data'
));
END;
It is a valid query that works just fine, I can run it with cursor.execute() with no errors.这是一个有效的查询,工作正常,我可以使用 cursor.execute() 运行它,没有错误。 But how to get the returned rows?
但是如何获取返回的行? I've tried the following:
我尝试了以下方法:
returntype = connection.gettype('MY_PKG_1.MY_DATA_TYPE')
cursor = connection.cursor()
results = cursor.callfunc('MY_PKG_2.MY_FUNC', returntype, ['some data', 'some more data'])
cursor.commit()
But I get an error "ORA-04043: object MY_PKG_1.MY_DATA_TYPE does not exist".但我收到错误“ORA-04043:对象 MY_PKG_1.MY_DATA_TYPE 不存在”。
If the PL/SQL block just does a single query, then execute that query directly without using PL/SQL:如果 PL/SQL 块只执行单个查询,则不使用 PL/SQL 直接执行该查询:
with connection.cursor() as cursor:
try:
for r in cursor.execute('SELECT * FROM TABLE(MY_PKG_2.MY_FUNC(param_1 => :p1, param_2 => :p2))', p1='some more data', p2='some data')
print(r)
except cx_Oracle.Error as e:
error, = e.args
print(error.message)
print(sql)
if (error.offset):
print('^'.rjust(error.offset+1, ' '))
Otherwise you will have to bind a REF CURSOR, or use Implicit Results.否则,您将必须绑定一个 REF CURSOR,或使用隐式结果。 See the cx_Oracle examples ref_cursor.py and implicit_results.py .
请参阅 cx_Oracle 示例ref_cursor.py和implicit_results.py 。
If you are returning collections etc, then this gives some insights into generically traversing them.如果您要返回集合等,那么这会提供一些有关一般遍历它们的见解。 But check the doc for simpler examples.
但是请查看文档以获取更简单的示例。
Also see the cx_Oracle doc on tuning fetch performance .另请参阅有关调整提取性能的 cx_Oracle 文档。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.