python数据库查询报错unsupported format character '?' (0x5df2) at index 1的解决方法

更新时间:2020-04-20 22:29:33

今天在使用python做爬虫的时候,有一个入库的操作。我一般会在入库前判断数据库中是否存在,存在的就不再重复入库了。

但是,我只要一使用查询命令(有结果的情况下),就会给我报错:unsupported format character '?' (0x5df2) at index 1,

如果没有结果的话不会报错,百度无解、Google误解,群里问无解!!!


只能继续Google,偶然间看到 https://labo.utsubo.tokyo/2017/05/19/valueerror-unsupported-format-character-y-0x59-at-index-51/ 这一篇文章,

但是不应该是format的问题啊:

image.png

我的代码很简单,不会有格式的问题,因为我有打印sql语句,是可以查询的,总感觉就是返回结果与python不兼容...


突然我就看到了上文中的utf-8:

image.png

于是我就给我代码加上了:

image.png

还是报错,那就改成gb2312


然后就解决了:

cursor.execute("set names utf-8")



突然想起来MySQL8与MySQL5.6之间需要把gb2312_chinese_ci转换一下,怎么转换我给忘记了,但是gb2312


问题解决,完美!

不过提示unsupported format character '?' (0x5df2) at index 1,感觉也没啥用啊 55555