[英]Python: ValueError: unsupported format character ''' (0x27) at index 1
I'm trying to execute a query to search 3 tables in a database using MySQL through Python. 我正在尝试执行查询以通过Python使用MySQL搜索数据库中的3个表。 Every time I try and execute the following string as a query, it gives me an error about concatenation in the string. 每次我尝试执行以下字符串作为查询时,都会给我关于字符串串联的错误。
"SELECT fileid FROM files WHERE description LIKE '%" + search + "%' OR filename LIKE '%" + search + "%' OR uploader LIKE '%" + search + "%' ORDER BY fileid DESC"
This is the error it gives me: 这是它给我的错误:
ValueError: unsupported format character ''' (0x27) at index 1
If I remove the character it asks for then I have to also remove the %, which stops the query from actually working properly. 如果删除要求的字符,则还必须删除%,这将阻止查询实际正常运行。 What can I do to fix this, since I'm rather new to Python. 由于我是Python的新手,我该怎么做才能解决此问题。
Thanks, Kris 谢谢,克里斯
It looks like python is interpreting the % as a printf-like format character. 看起来python正在将%解释为类似printf的格式字符。 Try using %%? 尝试使用%%?
"SELECT fileid
FROM files
WHERE description LIKE '%%%s%%'
OR filename LIKE '%%%s%%'
OR uploader LIKE '%%%s%%'
ORDER BY fileid DESC" % (search, search, search)
Just for you info: I tried the solution of @Pochi today, in Python 3.6, and for some reason it provoked not expected behaviour. 仅为您参考:我今天在Python 3.6中尝试了@Pochi的解决方案,由于某种原因,它引发了意外的行为。 I had two, and three arguments for format string, so at the end was: 我有两个和三个用于格式字符串的参数,所以最后是:
% (Search, Search)
My string ("search") began with an upper "S". 我的字符串(“搜索”)以大写“ S”开头。 I got the error message: 我收到错误消息:
ValueError: unsupported format character 'S' (0x53) at index 113
I changed uppercase to lowercase, and the error was: 我将大写更改为小写,错误是:
TypeError: not enough arguments for format string
Then I just put my arguments inside of double %% at the beginning and the end, and it worked. 然后,我只是在开头和结尾将参数放在双精度%%内,并且它起作用了。 So my code looked like: 所以我的代码看起来像:
"SELECT fileid
FROM files
WHERE description LIKE '%%search%%'
OR filename LIKE '%%search%%'
ORDER BY fileid DESC"
Another solution would be the one provided by @Alice Yuan. 另一个解决方案是@Alice Yuan提供的解决方案。 She just doubled the percentage sings, and it works. 她只是将演唱的百分比提高了一倍,而且效果很好。
you can try like this: 您可以这样尝试:
SELECT fileid
FROM files
WHERE description LIKE '%%%%%s%%%%'
OR filename LIKE '%%%%%s%%%%'
OR uploader LIKE '%%%%%s%%%%'
ORDER BY fileid DESC" % (search, search, search)
My solution: 我的解决方案:
query = """SELECT id, name FROM provice WHERE name LIKE %s"""
cursor.execute(query, '%%%s%%' % name)
I think it's easy way to fix this issue! 我认为这是解决此问题的简便方法!
The simplest answer is to add the LIKE wildcard character %
to the value. 最简单的答案是将LIKE通配符%
添加到该值。 This correctly quotes and escapes the LIKE pattern. 这将正确引用并转义为LIKE模式。
In Python 3.6+ you can use an f-string to include the LIKE wildcard character %
in the value which correctly inserts the escaped string value into the SQL: 在Python 3.6+中,您可以使用f字符串在值中包含LIKE通配符%
,以将转义的字符串值正确插入SQL:
# string to find, e.g.,
search = 'find-me'
# Parameterised SQL template
sql = """SELECT fileid FROM files
WHERE description LIKE %s OR filename LIKE %s OR uploader LIKE %s
ORDER BY fileid DESC"""
# Combine LIKE wildcard with search value
like_val = f'%{search}%'
# Run query with correctly quoted and escaped LIKE pattern
cursor.execute(sql, (like_val, like_val, like_val))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.