0

I am working on a simple program and attempting to pull some information out of a database. The table within the database that I am using is called names. If I do something like this:

self.cursor.execute("""select * from names WHERE first_name = 'Sarah' """)
for row in self.cursor.fetchall():
    print(row[0])

I get exactly what I want. But if I change that name to a variable like and do something like:

name = input('Enter First name: ')
self.cursor.execute("""select * from names WHERE first_name=%s""", (name))

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

I know that there are other posts on this site close to my question and what I have tried from them does not seem to work. I have also tried following some tutorials and still get this error. So any help would be appreciated! Thank you! I am using mysql.connector, Python 3.4 and Flask.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
ravenUSMC
  • 495
  • 5
  • 23
  • Does this answer your question? [Why do I get "TypeError: not all arguments converted during string formatting" when trying to use a string in a parameterized SQL query?](https://stackoverflow.com/questions/21740359/why-do-i-get-typeerror-not-all-arguments-converted-during-string-formatting-w) – Karl Knechtel Apr 04 '23 at 02:54

1 Answers1

0

Okay I found the answer myself. I had to do this: self.cursor.execute(query, (name,)) and the answer was found digging deeper and I found this site: Python MySQL Connector database query with %s fails

ravenUSMC
  • 495
  • 5
  • 23