简体   繁体   English

使用 python 脚本中的游标输出参数调用 oracle 存储过程

[英]Call oracle stored procedure with cursor output parameter from python script

I am trying to call a oracle stored procedure with 2 in and 1 out parameter from python script.我正在尝试从 python 脚本中调用一个带有 2 个输入和 1 个输出参数的 oracle 存储过程。 The problem I am having is passing a cursor out-parameter.我遇到的问题是传递一个游标输出参数。

The Oracle stored procedure is essentially: Oracle存储过程本质上是:

  PROCEDURE ci_lac_state 
     (LAC_ID_IN IN  VARCHAR2,  
      CI_ID_IN  IN  VARCHAR2 DEFAULT NULL,
      CGI_ID    OUT SYS_REFCURSOR)
  AS
  BEGIN
      OPEN cgi_id FOR
      ...
  END;

The python code calling to the database is:调用数据库的python代码是:

  #! /usr/bin/python

  import cx_Oracle

  lac='11508'
  ci='9312'

  try:
      my_connection=cx_Oracle.Connection('login/passwd@db_name')
  except cx_Oracle.DatabaseError,info:
      print "Logon Error:",info
      sys.exit()

  my_cursor=my_connection.cursor()
  cur_var=my_cursor.var(cx_Oracle.CURSOR)

  my_cursor.callproc("cgi_info.ci_lac_state", [lac, ci, cur_var])

  print cur_var.getvalue()

And I get such cursor value as the result:结果我得到了这样的游标值:

  <__builtin__.OracleCursor on <cx_Oracle.Connection to login@db_name>>

What am I doing wrong?我做错了什么?

Thanks.谢谢。

I've just had similar issue.我刚刚遇到了类似的问题。 cur_var has type <type 'cx_Oracle.CURSOR'> and cur_var.getvalue() gets object of type <type 'OracleCursor'> . cur_var类型为<type 'cx_Oracle.CURSOR'>并且cur_var.getvalue()获取类型为<type 'OracleCursor'> To get data you have to fetched them from the OracleCursor object.要获取数据,您必须从 OracleCursor 对象中获取它们。 Try for example:尝试例如:

print cur_var.getvalue().fetchall()

To see more function of OracleCursor object just check its directory:要查看 OracleCursor 对象的更多功能,只需检查其目录:

dir(cur_var.getvalue())

Hope this will help you!希望这会帮助你!

You can define your output and then loop through using an implicit cursor like this:您可以定义您的输出,然后使用这样的隐式游标循环:

outVal = cursor.var(cx_Oracle.CURSOR)

something = cursor.callproc(<your procedure name>, [outVal,<input variable here>])

for implicitCursor in cursor.getimplicitresults():
    for row in implicitCursor:
        
        fout.write(''.join(row)) # note: tuple conversion inside ()
        fout.write('\n')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python将Oracle Cursor作为参数传递给Oracle存储过程 - Python passing Oracle Cursor as parameter to Oracle Stored procedure 无法从 Python 脚本调用存储过程 - Unable to call stored procedure from Python script 使用游标在python中调用存储过程 - call stored procedure in python using cursor 在python中使用cursor.callproc()调用MySQL存储过程 - Call MySQL stored procedure using cursor.callproc() in python 从无人值守的 Python 调用 MySQL 存储过程 - Call a MySQL stored procedure from Python unattended 无法使用 Python 游标从存储过程返回结果 - Cannot return results from stored procedure using Python cursor Python调用带有表值参数的sql-server存储过程 - Python call sql-server stored procedure with table valued parameter 使用 Python 和 Cx_Oracle 调用带有 XMLTYPE 输入和输出参数的 Oracle 存储过程 - Calling Oracle stored procedure with XMLTYPE input and output parameters with Python and Cx_Oracle 如何检索存储过程的 output 参数? - How to retrieve an output parameter of a stored procedure? Python中的存储过程如何获取output参数? - How do you get output parameters from a stored procedure in Python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM