Python-Oracle 传递游标输出参数

6 投票
2 回答
9126 浏览
提问于 2025-04-16 22:14

我正在尝试在Python和Oracle数据库之间调用一个存储过程。现在遇到的问题是如何传递一个游标的输出参数。

这个Oracle存储过程大致是:

create or replace procedure sp_procedure(
    cid int, 
    rep_date date,
    ret out sys_refcursor
) is
begin

  open ret for 
  select  
 ...
  end;

调用数据库的Python代码是:

import cx_Oracle
from datetime import date

connstr='user/pass@127.0.0.1:2521/XE'
conn = cx_Oracle.connect(connstr)
curs = conn.cursor()

cid = 1
rep_date = date(2011,06,30)

curs.callproc('sp_procedure', (cid, rep_date, curs))

出现的错误是:

curs.callproc('sp_procedure', (cid, rep_date, curs))
cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

我还尝试过把一个字典作为关键字参数传递:

cid = 1
rep_date = date(2011,06,30)

call_params = {'cid': cid, 'rep_date': rep_date, 'ret': curs}
curs.callproc('sp_procedure', (cid, rep_date, curs), call_params)

结果还是出现同样的错误。

谢谢。

2 个回答

0

试试这个:

curs = con.cursor() 
out_curs = con.cursor() 

curs.execute("""
BEGIN 
    sp_procedure(:cid, :rep_date, :cur); 
END;""", cid=cid, rep_date=rep_date, ret=outcurs) 
13

经过几个小时的搜索和反复尝试,这里是解决方案:

cid = 1
rep_date = date(2011,06,30)

l_cur = curs.var(cx_Oracle.CURSOR)
l_query = curs.callproc('sp_procedure', (cid,rep_date,l_cur))

l_results = l_query[2]

for row in l_results:
    print row

# Column Specs
for row in l_results.description:
    print row

撰写回答