Kes115
Kes115

Reputation: 2150

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.

Upvotes: 1

Views: 1928

Answers (2)

Mr Ed
Mr Ed

Reputation: 75

The str.format() method is now used inplace of %s

    number = '+12345678901'
    cursor.execute('SELECT * FROM channel WHERE phone_number = {}').format(number))

Happened at about python2.6-ish, see https://docs.python.org/2/library/string.html?highlight=string.format#format-string-syntax

Upvotes: -2

zero323
zero323

Reputation: 330073

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, ))

Upvotes: 8

Related Questions