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

Jos*_*ian 3 python mysql mysql-python python-2.7

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

data='Teststr'
cur = db.cursor()
cur.execute("CALL test('{0}')".format(data))
Run Code Online (Sandbox Code Playgroud)

但是当我使用时

data='Teststr'
cur = db.cursor()
cur.callproc('test',data)
Run Code Online (Sandbox Code Playgroud)

我遇到了

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

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

Jon*_*nts 7

你想cur.callproc('test', (data,))传递一个元素的元组

例如:

>>> a = 'hello'
>>> len(a) # just a
5
>>> len( (a) ) # still just a
5
>>> len( (a,) ) # single element tuple containing a
1
Run Code Online (Sandbox Code Playgroud)