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)

ぐ巨炮叔叔 提交于 2020-06-17 15:50:12

问题


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.


回答1:


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())


来源:https://stackoverflow.com/questions/62230718/how-can-i-query-and-get-data-from-my-sqlite-database-even-when-my-search-input-h

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!