【问题标题】:Calling an Oracle DB function with IN, OUT, IN/OUT parameters and return value in Python在 Python 中使用 IN、OUT、IN/OUT 参数和返回值调用 Oracle DB 函数
【发布时间】:2021-11-19 16:02:19
【问题描述】:

我得到了一个可以调用如下的函数,即使用“SQL Oracle Developer”:

--set serveroutput on size 2000;
declare
    theId VARCHAR(20);
    theKey VARCHAR(20) := '';
    theName VARCHAR(20) := '';
    theEmail VARCHAR(20) := '';
    theDob DATE := '';
    theVal NUMBER;
begin
    theId := Package.FunctionName(
        theKey,
        theName,
        theEmail,
        theDob,
        theVal
    );
    --DBMS_OUTPUT.PUT_LINE(theId);
    --DBMS_OUTPUT.PUT_LINE(theKey);
    --DBMS_OUTPUT.PUT_LINE(theVal);
    exception
        when others then
            RAISE;
end;

现在,我应该使用 cx_Oracle 在 Python 中调用这个函数。

为此,我尝试了以下方法,但无法正常工作:

cursor = connection.cursor()
function = '''
    BEGIN
        theKey := inout_theKey;
        theName := in_theName;
        theEmail := in_theEmail;
        theDob := in_theDob;
        theVal := out_theVal;
        theId := Package.FunctionName(theKey, theName theEmail, theDob, theVal)
        :out_theId := theId;
        :inout_theKey := theKey;
        :out_theVal := theVal;
    END;'''

out_theId = cursor.var(str)
out_theVal = cursor.var(int)
inout_theKey = cursor.var(str)
inout_theKey.setvalue(0, '')
cursor.execute(
    function, 
    inout_theKey=inout_theKey, 
    in_theName='', 
    in_theEmail='', 
    in_theDob='', 
    out_theId=out_theId, 
    out_theVal=out_theVal)
_logger.debug(out_theId.getvalue())

这失败并出现错误:

cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number

也许有人可以指出我的绑定不正确的正确方向?


为了它的价值,我还尝试将 DECLARE 语句添加到 function ,即

function = '''
    DECLARE 
       theId VARCHAR(20) := out_theId;  
       theKey VARCHAR(20) := inout_ttheKey; 
       theName VARCHAR(20) := in_theName; 
       theEmail VARCHAR(20) := in_theEmail; 
       theDob DATE := in_theDob; 
       theVal NUMBER := out_theVal;  
    BEGIN  

       theId := Package.FunctionName(theKey, theName, theEmail, theDob, theVal); 

       :out_theId := theId; 
       :inout_theKey := theKey; 
       :out_theVal := theVal; 
    END;'''

这仍然为我提供了相同的结果 (illegal variable name/number)。

通过 cx_Oracle 与数据库的连接正在工作(使用其他几个 sql 语句验证)。

【问题讨论】:

  • 细节很重要。分享一整套我们可以运行的代码。包括 SQL 以创建必要的函数。见stackoverflow.com/help/minimal-reproducible-example
  • 嘿@ChristopherJones,谢谢您的回复!不幸的是,以上是我从给定功能中所知道的一切,因为它是由第三方提供给我的。但是,我现在能够回答我遇到的问题(绑定有什么问题/如何实际调用这样的函数)。

标签: python cx-oracle


【解决方案1】:

显然有一个相当简单的解决方案:

cursor = connection.cursor()
function = 'Package.FunctionName'

inout_theKey = ''
in_theName = ''
in_theEmail = ''
in_theDob = ''
out_theVal = cursor.var(int)  # default type is str, else define the type like this

out_theId = cursor.callfunc(function, str, [
    inout_theKey, 
    in_theName, 
    in_theEmail, 
    in_theDob, 
    out_theVal
])

_logger.debug(out_theId)
_logger.debug(inout_theKey)
_logger.debug(out_theVal)

基本上你只需要提供函数名称(function)、预期的返回值(在这种情况下为str)以及根据函数定义的 IN、OUT 和 IN/OUT 参数列表到 @ 987654326@电话。

参考:https://cx-oracle.readthedocs.io/en/latest/api_manual/cursor.html#Cursor.callfunchttps://cx-oracle.readthedocs.io/en/latest/user_guide/plsql_execution.html#plsqlfunc

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-18
    • 2014-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 2013-01-25
    相关资源
    最近更新 更多