Skip to content Skip to sidebar Skip to footer

Use Parameterized Query With Mysql.connector In Python 2.7

Im using Python 2.7 with mysql.connector running in pyCharm I need to use a parameterized query like shown here and also here Given those examples, it seems like cursor.execute('S

Solution 1:

The error you get is from mysql when it tries to execute the query. The query parameters passed to cursor.execute() need to be a tuple, you're passing a single value. To create a tuple with a single element you need to add a comma after the element:

cursor.execute("SELECT * FROM automatedReports WHERE pythonFunctionName = %s", (function_name,))

Otherwise mysql.connector doesn't escape anything and leaves the literal %s in the query.


Post a Comment for "Use Parameterized Query With Mysql.connector In Python 2.7"