0

I am trying to query the Database for a sub-string, I am substituting a string variable from a form for the query. It is giving me a syntax error.

This is the error:

LINE 1: SELECT * FROM books WHERE title LIKE '%'sometext'%'

This is the line:

books = db.execute("SELECT * FROM books WHERE title LIKE '%:form%'", {"form":form.search.data}).fetchall()

If I use an actual string in the SQL query it works. It is an issue with using a variable. Any assistance would be appreciated.

1 Answer 1

2

You need to concatenate the strings

  '%' + sometext + '%'
1
  • I used the CONCAT function. Thank you! books = db.execute("SELECT * FROM books WHERE title LIKE CONCAT('%',:form,'%'), {"form":form.search.data}).fetchall()
    – Pierre
    Commented Oct 16, 2019 at 10:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .