2

I am trying to select some values within a table that has a column called "Name". That column contains tennis players names. I want to store some statistics for each player in python, but I am having trouble accessing the table. I keep getting a "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 '='Rafael Nadal'' at line 1" As you can see, mysql clearly interprets the %s I had in place of 'Rafael Nadal' correctly, but it still brings up an error. Can anyone help me?

recordTuple = ('Rafael Nadal', )
mySql_insert_query = """SELECT `First_Serve(%)` FROM `serve2` WHERE Name =%s"""
cursor.execute(mySql_insert_query, recordTuple)
aI = cursor.fetchall()[0][0]/100
muth
  • 41
  • 6

1 Answers1

0

Since % is used to mark placeholders in the SQL, you have to double it if you want to use it literally.

mySql_insert_query = """SELECT `First_Serve(%%)` FROM `serve2` WHERE Name =%s"""

See How do I escape % from python mysql query

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks but that didnt work. It returns the error "Unknown column 'First_Serve(%%)' in 'field list'". I'm pretty sure its a problem with the last part of the query because when I replace the %s with just 'Rafael Nadal' it works perfectly. – muth Aug 28 '19 at 20:36
  • I've linked to another question like this. But maybe it's different in MySQL Connector than PyMySQL. – Barmar Aug 28 '19 at 20:40