Upchanges
Upchanges

Reputation: 338

How can I query and get data from my sqlite database even when my search input have similar words that is apart from each other (not continuously)

I have a blog search function on my website.

This is my search input: children lazy

I have a column in my SQLite database that is called: Children is getting lazy

I use the codes(Python) below to query the data:

many_posts0 = BlogPost.query.filter(or_((BlogPost.problem_name.ilike("%" + form.search.data + "%")),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())

However using the codes above, I cannot shows the result of the column "Children is getting lazy" with the search input "children lazy". If I make my search result as "children is", the column's data will show. I wonder if my problem is because of "ilike" in my query codes.

Upvotes: 0

Views: 132

Answers (1)

gelonida
gelonida

Reputation: 5630

split the sraach string by white space and join with '%'

# split words and join with "%"
search_string = "%" + "%".join(search_data.split()) + "%"
many_posts0 = BlogPost.query.filter(or_((BlogPost.problem_name.ilike(search_string)),(BlogPost.text.ilike("%" + form.search.data + "%")))).order_by(BlogPost.date.desc())

Upvotes: 1

Related Questions