1

I am working on a project that uses a HTML text input to retrieve data from a SQLite database. The idea goes like this : the user types string representing a product number and I look into my database for that string.

I have tried to make my query safe for SQL injection as suggested in this tutorial because the data does not come from me.

cursor.execute("SELECT product_number FROM price_history WHERE product_number = %s';", (user_input, ))

However, when I try to execute my code, I get :

sqlite3.OperationalError: near "%": syntax error
3
  • Why do you have a single quote after the %s? Commented Jul 5, 2021 at 23:25
  • I copy pasted the code from the tutorial and adjusted it to my database Commented Jul 5, 2021 at 23:33
  • The trailing ‘ needs to go Commented Jul 6, 2021 at 0:20

1 Answer 1

1

There's an extra ' after %s.

Read the first paragraphs of the python docs on sqlite3 that show the correct way to use placeholders.

cursor.execute("SELECT product_number FROM price_history WHERE product_number = (?)", (user_input, )) should work.

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.