Gwy*_*ell 7 python mysql mysql-python google-cloud-sql
我有一个托管在Google Cloud SQL上的数据库,以及一个用于查询它的python脚本.
我试图调用具有Out参数的存储过程.SP被成功调用,但Out参数的值似乎没有返回到我的python代码.
例如,下面是摘自的例子在这里:
乘法存储过程的定义:
CREATE PROCEDURE multiply(IN pFac1 INT, IN pFac2 INT, OUT pProd INT)
BEGIN
SET pProd := pFac1 * pFac2;
END
Run Code Online (Sandbox Code Playgroud)
如果我从命令行调用SP,如下所示:
CALL multiply(5, 5, @Result)
SELECT @Result
Run Code Online (Sandbox Code Playgroud)
我正确得到了结果:
+---------+
| @Result |
+---------+
| 25 |
+---------+
Run Code Online (Sandbox Code Playgroud)
但是如果我使用MySQLdb包用python代码调用它,就像这样:
args = (5, 5, 0) # 0 is to hold value of the OUT parameter pProd
result = cursor.callproc('multiply', args)
print result
Run Code Online (Sandbox Code Playgroud)
然后我没有得到我的结果元组中的out参数:
(5, 5, 0)
Run Code Online (Sandbox Code Playgroud)
那么,我在这里做错了什么?
更新:刚刚在callproc代码中发现此警告:
Run Code Online (Sandbox Code Playgroud)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元组.所以底线是不可能的.然后回到绘图板......
您只需要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),)
Run Code Online (Sandbox Code Playgroud)