使用 python 脚本中的游标输出参数调用 oracle 存储过程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19095690/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call oracle stored procedure with cursor output parameter from python script
提问by user2831245
I am trying to call a oracle stored procedure with 2 in and 1 out parameter from python script. The problem I am having is passing a cursor out-parameter.
我正在尝试从 python 脚本中调用一个带有 2 个输入和 1 个输出参数的 oracle 存储过程。我遇到的问题是传递一个游标输出参数。
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.
谢谢。
回答by kpater87
I've just had similar issue. cur_var
has type <type 'cx_Oracle.CURSOR'>
and cur_var.getvalue()
gets object of type <type 'OracleCursor'>
. To get data you have to fetched them from the OracleCursor object. Try for example:
我刚刚遇到了类似的问题。cur_var
具有类型<type 'cx_Oracle.CURSOR'>
并cur_var.getvalue()
获取类型的对象<type 'OracleCursor'>
。要获取数据,您必须从 OracleCursor 对象中获取它们。尝试例如:
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!
希望能帮到你!