Both mine and stuff solutions do not work for examples like (where there is two of more words from different db fields):

https://ide50-username.cs50.io/search?q=New+Haven,+Connecticut // http://pset8.cs50.net/search?q=New+Haven,+Connecticut

But work fine with just New+Haven or just Connecticut an so on.

My code:

def search():
"""Search for places that match query."""
q = request.args.get("q") + "%"
return jsonify(db.execute("SELECT * FROM places WHERE postal_code LIKE :q OR place_name LIKE :q OR admin_code1 LIKE :q OR admin_name1 LIKE :q LIMIT 10", q=q))

The value for q is the whole string after the = sign. So if you search was for example


Then inside python q becomes = 'New Haven, Connecticut'. So when the sql SELECT search is run its trying to match that whole q string with each individual column. so while "New Haven" may be a place_name in the places table "New Haven, Connecticut" would not match anything in that Column

  • right, I see. But why it LIKE doesn't work? Like with the index if you write 02... it returns all indexes that contain 02. Oct 29 '17 at 18:34
  • Artur, I'm having the same problem, I happen to enter the almost identical code. Here LIKE doesn't work because more information is provided to it unlike in the postal code example, it interprets information even when less information is provided but would fail if more digits are added to it. ex: 0231824, this would fail. Hope you understood. Mar 19 '18 at 18:02

You must log in to answer this question.

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