0

Code:

x = int(id_[0])
select_query = "SELECT * FROM files WHERE id = %s"
cursor.execute(select_query,x)
results = cursor.fetchone()

I am using mySQL in Python. This is the error I get:

mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '%s' at line

It works when I get rid of the variable and just add a random id like 1.

2
  • The second argument to cursor.execute() should be an iterable: cursor.execute(select_query,(x,)) Commented Apr 3, 2023 at 17:00
  • You definitely haven't quotes %s in your actual query? Commented Apr 3, 2023 at 17:01

1 Answer 1

0

You forgot to add string delimiters around your %s, so SQL is trying to process that as a variable name or column name. Furthermore, for regex expressions like the one you have, try using LIKE instead of =

To fix the SQL syntax errors, your query should look something like this:

x = int(id_[0])
select_query = "SELECT * FROM files WHERE id LIKE '%s'"
cursor.execute(select_query,x)
results = cursor.fetchone()
Sign up to request clarification or add additional context in comments.

Comments

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.