在python中调用存储过程时出错 - 使用MySQLdb

时间:2012-10-23 16:45:10

标签: python mysql python-2.7 mysql-python

我有一个名为test的MySQL存储过程,它接受一个参数。  我可以使用下面的代码

从python 2.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将每个字符视为一个参数。我在这里缺少什么?

1 个答案:

答案 0 :(得分:7)

您希望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
相关问题