[英]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.