简体   繁体   English

Python:ValueError:索引1处不支持的格式字符'''(0x27)

[英]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.

相关问题 传递参数以及在查询中使用通配符时索引处不支持的格式字符“”(0x27) - unsupported format character ''' (0x27) at index while passing parameter as well as using wildcard in query pd.read_sql - 不支持的格式字符错误 (0x27) - pd.read_sql - Unsupported format character error (0x27) Python,Django:ValueError:索引3处不受支持的格式字符&#39;(&#39;(0x28) - Python, Django: ValueError: unsupported format character '(' (0x28) at index 3 ValueError:索引处不支持的格式字符&#39;{&#39;(0x7b) - ValueError: unsupported format character '{' (0x7b) at index ValueError:索引3处不支持的格式字符&#39;&lt;&#39;(0x3c) - ValueError: unsupported format character '<' (0x3c) at index 3 ValueError: 不支持的格式字符 &#39;p&#39; (0x70) 在索引 7 - ValueError: unsupported format character 'p' (0x70) at index 7 ValueError:索引79处不支持的格式字符&#39;a&#39;(0x61) - ValueError: unsupported format character 'a' (0x61) at index 79 ValueError:索引 650 处不支持的格式字符 &#39;w&#39; (0x77) - ValueError: unsupported format character 'w' (0x77) at index 650 %d由python和附加ValueError:索引2处不支持的格式字符&#39;O&#39;(0x4f) - %d by python and addition ValueError: unsupported format character 'O' (0x4f) at index 2 ValueError: 不支持的格式字符 &#39;{&#39; (0x7b) at index 40 - Python &amp; CURL - ValueError: unsupported format character '{' (0x7b) at index 40 - Python & CURL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM