【问题标题】:Python-Oracle Passing in a Cursor Out ParameterPython-Oracle 传入游标输出参数
【发布时间】:2011-10-12 21:03:25
【问题描述】:

我正在尝试在 python 和 oracle db 之间调用存储过程。我遇到的问题是传递一个游标输出参数。

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)

返回相同的错误。

谢谢。

【问题讨论】:

    标签: python oracle cx-oracle database-cursor


    【解决方案1】:

    经过几个小时的谷歌搜索和跟踪/错误,解决方案如下:

    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
    

    【讨论】:

      【解决方案2】:

      试试这个:

      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) 
      

      【讨论】:

      • 嗨 Scott,我设法让 callproc() 方法工作。谢谢。
      猜你喜欢
      • 2016-02-02
      • 2011-10-26
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-08
      相关资源
      最近更新 更多