In my SQL table I stored the postal_code as INTEGER and place_name as TEXT. So whenever I type a number or text, it will match accordingly. The problem is that I'm not getting anything as JSON output.

Here's my code:

def search():
    """Search for places that match query."""
    a = []
    q = request.args.get("q")    #ex q= "Boca Raton, Florida, 33496"

    if type(q) == int:
        q = str(q) + "%"
        a= db.execute("SELECT * FROM places WHERE postal_code LIKE :q", q=q)
    elif type(q) == str:
        q += "%"
        a= db.execute("SELECT * FROM places WHERE place_name LIKE :q", q=q)

    return jsonify(a)

Output when testing with https://ide50-tadm123.cs50.io/search?q=33496:


Anyone know what I'm doing wrong? Really would appreciate any help. Thanks.

Edit: Here's a single row of my SQL table if anyone is wondering:

enter image description here

1 Answer 1


If q= "Boca Raton, Florida, 33496" as in the commented example, program will take the elif branch, and
a= db.execute("SELECT * FROM places WHERE place_name LIKE :q", q=q) will indeed return an empty list.

Try the query
SELECT * FROM places WHERE place_name LIKE 'Boca Raton, Florida, 33496 %'
in phpliteadmin or sqlite3 to see. There is no place_name that will match that string.

Does search give results if q is Boca?

As for the sample link with q=33496: request.args.get() method will always returns a string, unless you tell it otherwise, as with request.args.get("q",type=int) (documentation here). So q=33496 is also taking the elif branch, and the query returns no results.

Sorry there's no "change this to that" solution, you'll need to digest and analyze this information and modify the search function until you get the results you want.

  • Didn't know request.args.get() always returned a string. I simply am using .isdigit() to know if it's a number. Thanks a lot, I think it's working now
    – tadm123
    Feb 18, 2017 at 4:23

You must log in to answer this question.

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