3

我有一个名为 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 将每个字符视为一个参数。我在这里想念什么?

4

1 回答 1

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
于 2012-10-23T16:45:52.040 回答