Issue with pulling information from mysql in flask

Question:

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.

Asked By: ravenUSMC

||

Answers:

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

Answered By: ravenUSMC
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.