Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error When calling stored procedure in python - Using MySQLdb

I have a MySQL stored procedure called test which accepts one argument. I can execute the stored procedure from python 2.7x using below code

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

But when I use

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

I am encountering

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

Looks like python treats each character as one argument. What am I missing here?

like image 768
Jose Cherian Avatar asked Oct 21 '25 17:10

Jose Cherian


1 Answers

You want cur.callproc('test', (data,)) to pass a tuple of 1 element

eg:

>>> a = 'hello'
>>> len(a) # just a
5
>>> len( (a) ) # still just a
5
>>> len( (a,) ) # single element tuple containing a
1
like image 59
Jon Clements Avatar answered Oct 23 '25 06:10

Jon Clements



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!