I'm trying to execute a query to search 3 tables in a database using MySQL through Python. 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.
Thanks,
Kris
解决方案
It looks like python is interpreting the % as a printf-like format character. 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)
博主尝试用Python执行MySQL查询,查询字符串因格式字符报错,移除指定字符会使查询无法正常工作。解决方案是将%替换为%%,以避免Python将其解释为printf-like格式字符。
4807

被折叠的 条评论
为什么被折叠?



