1

Running this code

res = cursor.execute("SELECT `password` FROM `players` WHERE `username` = %s", usern) 

I get 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

5
  • 1
    Learn to use parameters and never have a problem like this again! Commented Jul 15, 2020 at 17:58
  • 1
    Are you sure that's the right paramstyle for the library you're using? Commented Jul 15, 2020 at 17:58
  • 1
    sql = """SELECT password FROM players WHERE username = %s """ res = cursor.execute(sql, usern) print (res) gives me same error Commented Jul 15, 2020 at 18:02
  • replace comma with '%', like "SELECT password FROM players WHERE username = %s" % usern Commented Jul 15, 2020 at 18:02
  • or you should use tuple : execute("...", (usern)) Commented Jul 15, 2020 at 18:07

1 Answer 1

3

The parametrized queries expect a tuple as an argument:

query = """SELECT `password` FROM `players` WHERE `username` = %s"""
res = cursor.execute(query, (usern, ))
Sign up to request clarification or add additional context in comments.

3 Comments

Note that's not a tuple.
Why not? Tuple can have only one member.
They can indeed, but that's not the syntax for it. The comma is the important part of the tuple, a one-tuple looks like (value,).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.