MySQLdb存储过程输出参数不起作用

时间:2014-06-10 11:29:10

标签: python mysql mysql-python google-cloud-sql

我有一个托管在Google Cloud SQL上的数据库,以及一个用于查询它的python脚本。

我正在尝试调用具有Out参数的存储过程。 SP被成功调用,但Out参数的值似乎没有返回到我的python代码。

例如,以下是取自here的示例:

乘法存储过程的定义:

CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
BEGIN
  SET pProd := pFac1 * pFac2;
END

如果我从命令行调用SP,请执行以下操作:

CALL multiply(5, 5, @Result)
SELECT @Result

我正确地得到了结果:

+---------+
| @Result |
+---------+
|      25 |
+---------+

但是如果我使用MySQLdb包用python代码调用它,就像这样:

args = (5, 5, 0) # 0 is to hold value of the OUT parameter pProd
result = cursor.callproc('multiply', args)
print result

然后我在结果元组中没有得到out参数:

(5, 5, 0)

那么,我在这里做错了什么?

更新: 刚刚在callproc代码中找到了这个警告:

    Compatibility warning: PEP-249 specifies that any modified
    parameters must be returned. This is currently impossible
    as they are only available by storing them in a server
    variable and then retrieved by a query. Since stored
    procedures return zero or more result sets, there is no
    reliable way to get at OUT or INOUT parameters via callproc.
    The server variables are named @_procname_n, where procname
    is the parameter above and n is the position of the parameter
    (from zero). Once all result sets generated by the procedure
    have been fetched, you can issue a SELECT @_procname_0, ...
    query using .execute() to get any OUT or INOUT values.

还要注意,callproc函数只返回相同的输入arg元组。所以底线是不可能的。然后回到绘图板......

2 个答案:

答案 0 :(得分:7)

您只需要一个额外的SELECT来访问输出值:

>>> curs.callproc('multiply', (5, 5, 0))
(5, 5, 0)
>>> curs.execute('SELECT @_multiply_0, @_multiply_1, @_multiply_2')
1L
>>> curs.fetchall()
((5L, 5L, 25L),)

答案 1 :(得分:0)

检查这个,只记得设置数据库连接只是为了MYSQL数据库初始化并尝试类似:

只是为了知道谈话的内容,数据库表定义:

CREATE TABLE table_tmp
(
    data1 INT(11),
    data2 VARCHAR(10),
    data3 TINYINT(1)  -- This will be the output value
);

数据库的定义过程:

DROP PROCEDURE IF EXISTS sp_test_tmp;
CREATE DEFINER=`<user_in_the_db>`@`%` PROCEDURE `sp_test_tmp`(

      IN in_data1   INT
    , IN in_data2   VARCHAR(10)
    , IN in_data3   BOOL
    , OUT result    BOOL
)
BEGIN
    INSERT INTO table_tmp
    (
         data1
        ,data2
        ,data3
    )
    VALUES 
    (
        in_data1
        ,in_data2
        ,in_data3
    );
  SET result = FALSE;  -- Setting the output to our desired value
  COMMIT;  -- This will help to update the changes in the database, with variable
           -- the row never will get updated (the select/get a little 
           -- complex less)
END;

Python代码 使用参数列表,我在考虑通用函数;)

TRUE = 1   -- My own definition, for make compatible Mysql and Python Boolean data representation
FALSE = 0

def execute_procedure(pname='sp_test_tmp',pargs=(1,'co@t.com',TRUE,FALSE)):
    try:
        cursor = mysql.connect().cursor()
        status = cursor.callproc(pname, pargs)
        cursor.execute('SELECT @_sp_test_tmp_3') # This is the magic
        result = cursor.fetchone()               # Get the Values from server

        if result[0] == TRUE:
           print ("The result is TRUE")
           resp = True
        elif result[0] == FALSE:
           resp = False
           print("The result is FALSE")
       else:
          resp = False
          print("This is crazy!!!")

       return str(resp)
       except Exception as inst:
         exception = type(inst)
         print(exception)
         return "DON'T"
       finally:
         cursor.close()