I'm trying to make search functionality to output all available books based on author/title or genre typed in a form (I'm using sqlite), using LIKE function.
The piece of code I'm using is as follows:
# User reached route via POST (submitting the form)
else:
query = request.form.get("query")
# Ensure query is not blank
if not query:
return apology("you haven't typed anything!", 400)
# Implementing search
books = db.execute("SELECT author, title, genre, id FROM books WHERE booked = 0 AND (author LIKE '%' || :query || '%' OR title LIKE '%' || :query || '%' OR genre LIKE '%' || :query || '%')",
query = query)
However, users often type query followed by a space (especially on mobile devices). And in this case the code doesn't work.
- How can I account for the situation with a space after the query and fix this?
- Is it possible to account for infinite number of spaces? E.g. if I print 2 spaces after?
Thanks!