9

I'm trying to retrieve data from a MySQL-database.

A = "James"
query = ("SELECT * FROM DB.tblusers WHERE UserName = %s ")
c = mysql.connector.connect(user='root', password='',
                          host='127.0.0.1',
                          database='DB')
cur1 = c.cursor()
cur1.execute(query, A)

Gives the following error message:

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 '%' at line 1

But the SQL works in the mySQL Workbench. Any ideas?

1 Answer 1

23

A should be a tuple, try with A = ("James",)

see documentation of MySQLCursor.execute(operation, params=None, multi=False)


EDIT: added a comma, thanks to "swordofpain" (I learned something)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot! Didn't work "alone", but added another criteria (department) and then it's ok.

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.