如何使用cx_Oracle调用带有日期输入的过程返回游标

时间:2017-10-04 10:04:28

标签: python database oracle cx-oracle

我需要使用日期来查询对象,其中结果作为refcursor返回,如果我只是将日期添加到我返回的函数中(日期不可以)。我已经尝试将时间作为一个字符串发送,但也不起作用,我得到一个ORA-01830: date format picture ends before converting entire input string

程序我正在使用

PROCEDURE prc_get_some_data(
    p_date          IN  DATE,
    p_cursor        OUT SYS_REFCURSOR)
IS
BEGIN
    DBMS_APPLICATION_INFO.SET_CLIENT_INFO ('Python Script');
    OPEN p_cursor FOR
        SELECT * 
        FROM table
        WHERE creates = p_date;
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 __enter__(self):
        self.__db = cx_Oracle.connect('/@wallet_string')
        self.__cursor = self.__db.cursor()
        return self

    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",[datetime.strptime("2007-12-31 23:59:59", "%Y-%m-%d %H:%M:%S"), l_cur])[0]


        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__":

    with Test() as mydbconn:
        for row in mydbconn.get_corporate_actions_equities():
            print row

数据库oracle 12c 客户:12.1.0.2 任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:0)

感谢一位同事 ret_cursor应该是callproc返回的元组中的最后一项。所以不是[0],而是[-1]。

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
    rep_date = date(2017,07,20)
    l_test = self.__cursor.callproc("prc_get_some_data",[rep_date, l_cur])[-1]


    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)