Skip to content
Advertisement

MySQL Query Passing Two Variables Python SQLConnector

I can’t seem to get two values passed on a query, I can get one variable to pass no problem, can’t figure out what I’m doing wrong, error posted when I try to use the two variable code, one variable code passes.

One Variable:

JavaScript

Two Variable:

JavaScript

Error:

JavaScript

Updated code after answer:

JavaScript

Error:

JavaScript

I am not sure I still get this error

Advertisement

Answer

Place you parameters in a tuple, like this:

JavaScript

The MySQL Connector cursor.execute method accepts three parameters: the SQL statement, parameters and a flag (multi). When you execute

JavaScript

the final parameter is assumed to be the multi flag. It’s best practice to always pass parameter values as a tuple*, even if there is only one ((param,)). You may get other errors otherwise.


* You can use a list also, but using a tuple is conventional.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement