表里面的数据:

Python查询数据库,中文的结果显示不出来

 

问题:查询数据库,返回结果不是中文可以,是中文的话就报错UnicodeEncodeError: 'gbk' codec can't encode character '\xd4' in position 5: illegal multibyte sequence

源码:

#coding:utf-8
import pymysql

conn = pymysql.connect(
    host = 'localhost',
    port = 3306,
    user = 'root',
    passwd = 'mysql',
    db = 'cltest'
)

cur = conn.cursor()
sql = "select id,status from cost_cl where id in (1,2)"
cur.execute(sql)
rows = cur.fetchall()
for dr in rows:
    print(dr)

这个是ok的

Python查询数据库,中文的结果显示不出来

 

把查询语句改成

Python查询数据库,中文的结果显示不出来

 

Python查询数据库,中文的结果显示不出来

 寻找原因:

方法一:不适用我,我加入会报错

import sys

reload(sys)

Python查询数据库,中文的结果显示不出来

 

方法二:也不行

Python查询数据库,中文的结果显示不出来

方法三:

查看我本机的mysql编码格式

Python查询数据库,中文的结果显示不出来

根据https://jingyan.baidu.com/article/03b2f78c68b0c15ea237ae8d.html修改字符格式

Python查询数据库,中文的结果显示不出来此时依然没用

 方法四:加了gbk编码,此时有返回结果,但是是乱码

Python查询数据库,中文的结果显示不出来

Python查询数据库,中文的结果显示不出来

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
  • 2021-11-21
  • 2021-12-02
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-04
  • 2022-12-23
  • 2021-07-15
  • 2022-12-23
相关资源
相似解决方案