【问题标题】:Return a stored procedure out cursor variable in cx_oracle在 cx_oracle 中返回存储过程 out 游标变量
【发布时间】:2018-03-13 10:10:44
【问题描述】:

我正在尝试使用 cx_oracle 从 python 中的过程返回一个引用光标,我的过程如下所示。该过程下方是我尝试使用的python。当我运行脚本时,返回的只是

DB:       0.00400018692017  seconds
Total:    0.00400018692017  seconds
<__builtin__.OracleCursor on <cx_Oracle.Connection to connection_string>>

如何遍历 refcursor ? oracle documentation

PROCEDURE prc_get_some_data(
    p_cursor        OUT SYS_REFCURSOR)
IS
BEGIN
    DBMS_APPLICATION_INFO.SET_CLIENT_INFO ('Python Script');
    OPEN p_cursor FOR
        SELECT * 
        FROM table;
EXCEPTION
    WHEN OTHERS
        THEN
            DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_STACK);  
            DBMS_OUTPUT.PUT_LINE(CHR(10));  
            DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);  
END prc_get_somedatas;

python 脚本

import sys, time
from datetime import date, timedelta
import random
import cx_Oracle
import string

# Python Script to call a PL/SQL stored procedure to bulk load data



class Test:
    def __init__(self):
        self.__db = cx_Oracle.connect('connection_string')
        self.__cursor = self.__db.cursor()

    def __exit__(self, type, value, traceback):
        self.__cursor.close()
        self.__db.close()


    def get_some_data(self):
        d=date.today() # Initialize a date variable with date of today
            
        db_start = time.time() 
        start_all = time.time()

        self.__cursor.bindarraysize = 2500 
        self.__cursor.arraysize = 10000 

        # create a cursor variable to return the results into
        l_cur = self.__cursor.var(cx_Oracle.CURSOR)

        #execute the procedure
        l_test = self.__cursor.callproc("prc_get_some_data",[l_cur])

                       
        db_elapsed = (time.time() - db_start)
        total_elapsed = (time.time() - start_all)

        print "DB:\t ", db_elapsed, " seconds"  
        print "Total:\t ", total_elapsed, " seconds"  

        return list(l_test)



if __name__ == "__main__":
    test = Test()
    print test.get_some_data()

数据库 oracle 12c 客户端:12.1.0.2

【问题讨论】:

    标签: python database oracle stored-procedures cx-oracle


    【解决方案1】:

    对过程的调用接受一个序列作为参数,并返回一个序列。

    l_test = self.__cursor.callproc("prc_get_some_data",[l_cur])
    print(type(l_test))
    #>>> <class 'list'>
    

    所以你可以通过索引访问返回的游标:

    ret_cursor = self.__cursor.callproc("prc_get_some_data",[l_cur])[0]
    

    l_test = self.__cursor.callproc("prc_get_some_data",[l_cur])
    ret_cursor = l_test[0]
    

    然后你可以用for循环打印结果

    for line in ret_cursor:
        print line
    

    或使用 print ret_cursor.fetchall() ,或使用 pprint 工具(如果需要)。


    在你链接的documentation中,返回值直接解包为l_queryl_emp

    l_query, l_emp = self.__cursor.callproc("PKG_HR.FIND_EMPLOYEES", [p_query, l_cur])
    

    顺便说一句,您可能需要在最后关闭返回的游标,方法与主游标相同:ret_cursor.close()。否则它会抛出一个关于 connection cannot be closed 的异常。

    【讨论】:

      猜你喜欢
      • 2012-11-29
      • 2011-10-16
      • 1970-01-01
      • 1970-01-01
      • 2011-01-24
      • 2013-12-19
      • 1970-01-01
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多