ValueError(“索引185处不支持的格式字符'A'(0x41)”,)

时间:2023-01-14 16:05:26

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:

我正在尝试执行一个查询,该查询列出了我的数据库的某些值,这些值与我的html搜索表单的输入相匹配。我希望它进行搜索,即使一些表单输入是空输入。我使用的是Python 3.4,我的查询是这样的:

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

如果你知道如何解决这个问题,我真的很感激。我是数据库的先驱。提前致谢

1 个解决方案

#1


0  

Placeholders for parameters are spelled %s, not %:

参数的占位符拼写%s,而不是%:

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.

请注意,不应在占位符周围添加引号(将其留在数据库驱动程序中),对于LIKE查询,需要将通配符添加到参数值。我在这里使用str.format()在每个值之前和之后放置%通配符。没有通配符的LIKE查询(所以%或_)也可以使用= equality tests。

#1


0  

Placeholders for parameters are spelled %s, not %:

参数的占位符拼写%s,而不是%:

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.

请注意,不应在占位符周围添加引号(将其留在数据库驱动程序中),对于LIKE查询,需要将通配符添加到参数值。我在这里使用str.format()在每个值之前和之后放置%通配符。没有通配符的LIKE查询(所以%或_)也可以使用= equality tests。