Reputation: 43
I am trying to execute a query which lists certain values of my db that match the inputs of my html search form. I want it to do the search even if some of the form inputs are empty inputs.I am using Python 3.4 and my query is something like this:
query=("select DISTINCT val1,val2,val3,val4 from tab1,tab2 WHERE tab1.val1=tab2.val1 AND onoma LIKE '%' AND epitheto LIKE '%' AND (val4 between % AND %)" )
data =(d1, d2, d3 ,d4)
c.execute("SET NAMES utf8")
c.execute(query,data)
the error I get is this:
ValueError("unsupported format character 'A' (0x41) at index 185",)
Please, if you know how i can fix this, i would really appreciate it. I'm a begginer with databases. Thanks in advance
Upvotes: 2
Views: 2403
Reputation: 1121166
Placeholders for parameters are spelled %s
, not %
:
query = """
select DISTINCT val1,val2,val3,val4 from tab1,tab2
WHERE tab1.val1=tab2.val1 AND onoma LIKE %s AND
epitheto LIKE %s AND
(val4 between %s AND %s)
"""
data = ('%{}%'.format(d1), '%{}%'.format(d2), d3 ,d4)
c.execute(query, data)
Note that you should not add quotes around placeholders (leave those to the database driver), and for a LIKE
query, you need to add the wildcards to the parameter value. I used str.format()
here to put %
wildcards before and after each value. A LIKE query without wildcards (so %
or _
) may as well just use =
equality tests.
Upvotes: 1