在Python中调用存储过程时出错-使用MySQLdb [英] Error When calling stored procedure in python - Using MySQLdb

查看:114
本文介绍了在Python中调用存储过程时出错-使用MySQLdb的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为test的MySQL存储过程,它接受一个参数。 我可以使用以下代码从Python2.7x执行存储过程

data='Teststr'
cur = db.cursor()
cur.execute("CALL test('{0}')".format(data))

但当我使用

data='Teststr'
cur = db.cursor()
cur.callproc('test',data)

我遇到了

OperationalError: (1318, 'Incorrect number of arguments for PROCEDURE MyDb.test; expected 1, got 7')

看起来,Python将每个字符视为一个参数。我在这里错过了什么?

推荐答案

您希望cur.callproc('test', (data,))传递一个包含1个元素的元组

例如:

>>> a = 'hello'
>>> len(a) # just a
5
>>> len( (a) ) # still just a
5
>>> len( (a,) ) # single element tuple containing a
1

这篇关于在Python中调用存储过程时出错-使用MySQLdb的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆