Python 3.4 mysql SELECT / WHERE query - syntax problem
I have a simple form which passes a single entered name to an action page. The action page picks up the name entered and uses it in the function below. The objective is to identify and print out information on those records which have the form entered name.
The database name is office, the table being queried is members2
Below is the error message I am receiving. You will see that the name KANE is included in the error message - This is the name entered into the form for this particular test - proving to me that the form entry is available to the mysql query below.
Appreciate if you could have a look at the syntax - hopefully something obvious to the experienced eye !
many thanks
james
mysql query
def selectpeople(db,cursor): sql = ("""select * from members2 where nom = %s""" %(name)) cursor.execute(sql) all_names = cursor.fetchall() return all_names
ERROR MESSAGE
mysql.connector.errors.ProgramingError: 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 '['kane', 'Submit']' at line 2
PythonMySQL Server
Last Comment
Dave Baldwin
8/22/2022 - Mon
Dave Baldwin
Try using single quotes around the %s like: '%s' That's what is normally expected in a MySQL query. My little MySQL Py demo has the sql string set up differently.
sql = "select * from members2 \ where nom = '%s' " %(name))
Thanks for looking at this Dave. No luck so far I'm afraid. I have tried your string set up with and without the " on the %s. I show the strings and the error codes below. I have also tried the string set up I used with and without the '' on the %s and I show the strings and error codes below.
As you know, I am using python 3 and my reading of the results below suggests that the %s should not be bracketed. The strings in the error messages seem distorted when the " are used ??
But either way, the problem is not solved. If I forget the WHERE clause ... sql = ("SELECT * FROM members2")..
I do get the full set of data printed out.
Below is the function which I use for printing out the data - maybe there is a problem there when the WHERE clause is used?
def displayAll_names(all_names): print("<table border='1'>") print("<tr>") print("<th>Member_ID</th>") print("<th>civilities</th>") print("<th>PreNom</th>") print("<th>Nom</th>") print("<th>adresse</th>") print("</tr>") for each in all_names: print("<tr>") print("<td>{0}</td>".format(each[0])) print("<td>{0}</td>".format(each[1])) print("<td>{0}</td>".format(each[2])) print("<td>{0}</td>".format(each[3])) print("<td>{0}</td>".format(each[4])) print("</tr>") print("</table>")
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 '['kane', 'Submit']' at line 1
sql = ("select * from members2 \
where nom = %s " %(name))
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 'kane', 'Submit']'' at line 1
sql = ("select * from members2 \
where nom = '%s' " %(name))
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 '['kane', 'Submit']' at line 2
sql = ("""SELECT * FROM members2
WHERE nom = %s """ %(name))
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 'kane', 'Submit']'' at line 2
sql = ("""SELECT * FROM members2
WHERE nom = '%s' """ %(name))
Open in new window