0

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)

1 Answer 1

1

Here's a way to see what the problem is using command line python:

Python 3.4.3 (default, Nov 17 2016, 01:08:31) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> q="Bowling Green, Florida"
>>> q = q.split(",")
>>> q

I leave it to you to evaluate the result of >>> q to see where the problem lies. The final frontier :)

1
  • Thanks for the help! And good hint XD
    – Jason_V
    Jul 18, 2017 at 15:40

You must log in to answer this question.

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