cx_oracle:callfunc可以返回列表吗?

发布于 2024-12-14 11:59:49 字数 1342 浏览 2 评论 0原文

我正在尝试编写一个返回整数数组的 PL/SQL 函数,然后能够使用 cx_Oracles callfunc 调用它。我认为我的 PL/SQL 函数是正确的,但我不知道如何用 cx_Oracle 调用它。

创建或替换类型 test_type is table of NUMBER(10);

create or replace function test_function (n in INTEGER)
RETURN test_type
AS
  tmp_tab test_type := test_type();
BEGIN
  tmp_tab.EXTEND(n);
  FOR i IN 1 .. n LOOP
    tmp_tab(i) := i;
  END LOOP;
  RETURN tmp_tab;
END;

它与 sqlplus 一起使用:

SQL> select test_function(20) from dual;

TEST_FUNCTION(20)
--------------------------------------------------------------------------------
TEST_TYPE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

如何使用 cx_Oracle 获取此类函数的结果?这可能吗?

我找到了这个 http://osdir.com/ ml/python.db.cx-oracle/2005-06/msg00014.html 但我真的不知道如何使用它。当我将类型定义更改为:

create or replace type test_type is table of NUMBER(10) index by binary_integer;

我得到: 警告:创建的类型有编译错误。

SQL> sho err
Errors for TYPE TEST_TYPE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0  PL/SQL: Compilation unit analysis terminated
1/19     PLS-00355: use of pl/sql table not allowed in this context

I'm trying to write a PL/SQL function that returns an array of integers and then be able to call it with cx_Oracles callfunc. I think I got the PL/SQL function right, but I don't know how to call it with cx_Oracle.

create or replace type test_type is table of NUMBER(10);

create or replace function test_function (n in INTEGER)
RETURN test_type
AS
  tmp_tab test_type := test_type();
BEGIN
  tmp_tab.EXTEND(n);
  FOR i IN 1 .. n LOOP
    tmp_tab(i) := i;
  END LOOP;
  RETURN tmp_tab;
END;

It works with sqlplus:

SQL> select test_function(20) from dual;

TEST_FUNCTION(20)
--------------------------------------------------------------------------------
TEST_TYPE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

How can I get the result of such function using cx_Oracle? Is that possible?

I've found this http://osdir.com/ml/python.db.cx-oracle/2005-06/msg00014.html but I don't realy know how to use it. When I change my type definition to:

create or replace type test_type is table of NUMBER(10) index by binary_integer;

I get:
Warning: Type created with compilation errors.

SQL> sho err
Errors for TYPE TEST_TYPE:

LINE/COL ERROR
-------- -----------------------------------------------------------------
0/0  PL/SQL: Compilation unit analysis terminated
1/19     PLS-00355: use of pl/sql table not allowed in this context

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

西瓜 2024-12-21 11:59:50

内容

import cx_Oracle

db_sid = 'db_sid'
db_usr = 'schema'
db_pwd = 'passwd'

conn_data = str('%s/%s@%s') % (db_usr, db_pwd, db_sid)

try:
    db = ora.connect(conn_data)
    except ora.DatabaseError, e:
    error, = e
    ORAmessage = error.message.rstrip("\n")
    print "DatabaseError: %s" % ORAmessage
else:
    cursor = db.cursor()
    try:
        out_parameter = cursor.var(cx_Oracle.NUMBER)
        # calling function to retrieve results until 20
        execute_func  = cursor.callfunc('test_function', out_parameter, [20])
        print str(return_value.getvalue())
    except ora.DatabaseError, exc:
        error, = exc
        ORAmessage = error.message.rstrip("\n")
        print "DatabaseError: %s" % ORAmessage
    cursor.close()

db.close()

阅读这部分的
手册也很有用。

Something in the lines of,

import cx_Oracle

db_sid = 'db_sid'
db_usr = 'schema'
db_pwd = 'passwd'

conn_data = str('%s/%s@%s') % (db_usr, db_pwd, db_sid)

try:
    db = ora.connect(conn_data)
    except ora.DatabaseError, e:
    error, = e
    ORAmessage = error.message.rstrip("\n")
    print "DatabaseError: %s" % ORAmessage
else:
    cursor = db.cursor()
    try:
        out_parameter = cursor.var(cx_Oracle.NUMBER)
        # calling function to retrieve results until 20
        execute_func  = cursor.callfunc('test_function', out_parameter, [20])
        print str(return_value.getvalue())
    except ora.DatabaseError, exc:
        error, = exc
        ORAmessage = error.message.rstrip("\n")
        print "DatabaseError: %s" % ORAmessage
    cursor.close()

db.close()

Reading this part of the
manual will also be useful.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文