Shabina Rayan
Shabina Rayan

Reputation: 419

Calling MySQL Stored Procedure in Python - incorrect # of arguments error

I am trying to access a stored procedure using Python where I enter enter in a customer ID, and it will render a multi-row table with the multiple instances a customer ate at a restaurant as well as what they ate, what time they ate it, and what month they ate it. It is a multi-row table since the customer ID is not the primary key.

Within SQL, entering in this command:

call metrics.GetFoodByCustomerID("115960981")

will render the correct table for me. However when I run the following function in Python, I am getting the error that I am entering the incorrect arguments for procedure.

[enter image description here]

Any idea what I am missing?

Upvotes: 1

Views: 2942

Answers (1)

Luke Woodward
Luke Woodward

Reputation: 64949

The documentation for cursor.callproc states that the second parameter args should be a

Sequence of parameters to use with procedure

In your line

myCursor.callproc('metrics.GetFoodByCustomerID', ('115960981'))

you are passing in a string surrounded by parentheses for args. The parentheses around '115960981' have no effect, in particular they don't turn this value into a 1-element sequence. However, in Python, strings are sequences, so the parameter list is being interpreted as a sequence of 9 one-character strings. The error arises because your stored procedure doesn't take 9 arguments.

Try replacing the above line with

myCursor.callproc('metrics.GetFoodByCustomerID', ('115960981',))

This is probably what you were aiming for. Note the trailing comma, which makes the expression in parentheses into a 1-tuple. Alternatively, use a list instead: you don't need a trailing comma in order to make a 1-element list:

myCursor.callproc('metrics.GetFoodByCustomerID', ['115960981'])

Upvotes: 2

Related Questions