I'm trying to make a search bar that continually narrows down rows in my database depending on the string used. I want to be able to have my search contain multiple words and have each word search different column.
example.db
Make Model Year
------ ------ ------
Toyota Corolla 2000
Toyota Corolla 2002
Toyota Camry 2000
Toyota Camry 2002
Honda Civic 2002
This is what I have so far...
@app.route("/search")
def search():
q = request.args.get("q") #q is a string containing make, model, and/or year
car = db.execute("SELECT * FROM places WHERE Make LIKE :q\
OR Model LIKE :q\
OR Year LIKE :q, q=q+"%")
Right now, this works if I only search for one column. If I have /search?q=corolla, it will select the two rows with model corolla. If I have /search?q=toyota+corolla, it will select no rows, since no single column has toyota corolla.
I want to make it so that the more information I enter into q, the narrower my search results would be
ie.
- /search?q=toyota returns 4 rows
- /search?q=toyota+corolla returns 2 rows
- /search?q=toyota+corolla+2000 returns 1 row
I've been googling around for a while, but I can't seem to word my question correctly. So far, I've tried splitting the string by spaces and querying for each word, but that was just the same as searching them individually. I also tried adding a wild card in front of q, but that didn't solve the problem of having multiple words in the search at all. I'm completely stumped on what feels to be something simple. The input will always be in the order of make, model, year if that matters. Any help appreciated.