Search code examples
pythonmysqlmysql-connector-python

Connector/Python Select statement not working


Could someone please help me figure out what's wrong with the following code. It's a Python application that's trying to select from a mysql database using Connector/Python

number = '+12345678901'
cursor.execute('SELECT * FROM channel WHERE phone_number = %s', (number))

It's producing the following error:

1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '%s' at line 1

When I check the mysql log, I see the following:

SELECT * FROM channel WHERE phone_number = %s

This means it's not substituting the phone number.


Solution

  • According to Python Database API parameters should be provided as sequence or mapping and (number) is neither one.

    To create a single element tuple you need a comma after the element.

    cursor.execute('SELECT * FROM channel WHERE phone_number = %s', (number, ))