Bk Razor
Bk Razor

Reputation: 1532

Can not get my parameter to work inside SELECT WHERE clause in python

So I made a function that takes as a parameter a string and then uses a select where clause to get the values for just that parameter, but for some reason it will not work. I've tried (ar), "ar", '%s'%ar None of those are working for me please help

def getLoc(ar):
    cursor = conn.execute("SELECT location.name from area,location WHERE area.name = ***(ar)*** ")


get_locations_for_area("Columbia")
conn.close()

Upvotes: 0

Views: 79

Answers (3)

xthestreams
xthestreams

Reputation: 169

Do this:

def getLoc(ar):
    cursor = conn.execute("SELECT location.name from area,location WHERE area.name = ?", ar)

Check out the documentation https://docs.python.org/2/library/sqlite3.html, it tells you why to use the "?"

Upvotes: 2

NEERAJ CHAUDHARY
NEERAJ CHAUDHARY

Reputation: 29

This is how you should use this statement

cursor = conn.execute("SELECT location.name from area,location WHERE area.name = %s" %ar)

Upvotes: 0

Luis F Hernandez
Luis F Hernandez

Reputation: 931

use the code formatting to make your question easier to read! I think the way it would work is:

def getLoc(ar):
    cursor = conn.execute("SELECT location.name FROM area,location WHERE area.name = ?", (ar,))

What this does is, make the question mark correspond to the second argument of conn.execute(). Basically it's to put variables in as parameters of your query.

Upvotes: 2

Related Questions