1

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?

1
  • 1
    In future, please don't post images of code. Please copy and paste the text of your code into your question. I wanted to copy a line of code from your question into my answer but I couldn't: I had to type it out manually. See also meta.stackoverflow.com/questions/285551/… Commented Jun 22, 2017 at 19:38

1 Answer 1

2

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'])
Sign up to request clarification or add additional context in comments.

1 Comment

@ShabinaRayan: I don't know where you got the idea you could call stored_results on a cursor. That attribute isn't mentioned on the documentation page I linked to, so frankly I'm not surprised you're getting that error. Perhaps you want to use one of the fetchall, fetchone or fetchmany methods in the documentation I linked to?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.