9

I'm referencing this page in order to make a SELECT query to my database. However, I'm getting this error:

mysql.connector.errors.ProgrammingError: 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

Here's the section of code with the problem:

import mysql.connector
cnx = mysql.connector.connect(user='xxxx', password='yyyy',
                              host='zzzz.us-west-2.rds.amazonaws.com',
                              database='iiii')
cursor = cnx.cursor()

# ...

givenUsername = 'testUser123'
checkUserAuthQuery = ("SELECT password FROM UserAuth WHERE username = %s")
userAuthInfo = (givenUsername)
cursor.execute(checkUserAuthQuery, userAuthInfo)

# ...

Notes:
- When doing an INSERT query with %s it works.
- Also, replacing %s with 'testuser123' works.

cid
  • 447
  • 2
  • 7
  • 15

1 Answers1

27

You are missing the comma to make userAuthInfo a tuple. Change it to :

userAuthInfo = (givenUsername,)
Alasdair
  • 298,606
  • 55
  • 578
  • 516
  • 1
    This should be closed either as a duplicate of one of the *many* missing comma Q/A, or as simple typo. – Ilja Everilä May 30 '17 at 16:35
  • 2
    @IljaEverilä I tapped out the answer from my phone, so it was easier to write the answer than search for a duplicate. If you find a suitable duplicate target then I'll vote to close. – Alasdair May 30 '17 at 16:45
  • 1
    Sorry for the original tone. Here's a good candidate, I think: https://stackoverflow.com/questions/23600286/python-mysql-connector-database-query-with-s-fails . I spent my vote on "typo" already, so adding the link here for you. – Ilja Everilä May 30 '17 at 16:57