I'm trying to implement search in pset8 so that it can narrow down searches based on whether a comma followed by a state name or symbol is included. For example, the standard search would output many locations for "Bowling Green", while my search should output one location for "Bowling Green, Florida". Sadly, my code is not working as expected-it always outputs an empty array whenever I follow a city with a comma and state. I am sure that the first conditional is entered when a comma is present, yet so the problem is probably with the first db.execute statement. But for the life of me, I can't figure out what's wrong with it! Here's the code:
def search():
"""Search for places that match query."""
q = request.args.get("q")
comma_count = q.count(",")
if comma_count == 1:
q = q.split(",")
place = db.execute("SELECT * from places WHERE place_name = :q1 AND (admin_name1 = :q2 OR admin_code1 = :q2)", q1=q[0], q2=q[1])
return jsonify(place)
else:
q = q + "%"
place = db.execute("SELECT * from places WHERE postal_code LIKE :q OR place_name LIKE :q OR admin_name1 LIKE :q OR admin_code1 LIKE :q", q=q)
return jsonify(place)