Python:ValueError:索引1处不支持的格式字符'''(0x27) [英] Python: ValueError: unsupported format character ''' (0x27) at index 1

查看:593
本文介绍了Python:ValueError:索引1处不支持的格式字符'''(0x27)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行查询以通过Python使用MySQL搜索数据库中的3个表.每次我尝试执行以下字符串作为查询时,都会给我关于字符串串联的错误.

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"

这是给我的错误:

ValueError: unsupported format character ''' (0x27) at index 1

如果我删除了它要求的字符,那么我还必须删除%,这将阻止查询实际正常工作.由于我是Python的新手,我该怎么做才能解决此问题.

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.

谢谢, 克里斯(Kris)

Thanks, Kris

推荐答案

似乎python将%解释为类似于printf的格式字符.尝试使用%%?

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:ValueError:索引1处不支持的格式字符'''(0x27)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆