3

python3 mysqlclient1.4.2在centos7 执行 `select * from table where a = '中文';`...

 1 year ago
source link: https://qidizi.github.io/python3-mysqlclient-centos%E8%A7%A6%E5%8F%91latin-1%E5%BC%82%E5%B8%B8/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

python3 mysqlclient1.4.2在centos7 执行 `select * from table where a = '中文';` 出现了`UnicodeEncodeError: 'latin-1' codec can't encode characters`异常

在连接时设置一下 charset='utf8'即可解决


_me._mysql = _mysql = Mysql.connect(
                host=ConfigCustom.get_mysql_host(),
                password=ConfigCustom.get_mysql_pwd(),
                port=ConfigCustom.get_mysql_port(),
                user=ConfigCustom.get_mysql_user(),
                db=ConfigCustom.get_mysql_db(),
                use_unicode=True,
                # 否则有些系统会触发
                # UnicodeEncodeError: 'latin-1' codec can't encode characters in position 34-35:
                # ordinal not in range(256)
                charset='utf8',
                connect_timeout=30,  # 这个参数好像无效,还是默认使用60来
                autocommit=True,  # 它默认是否
            )
            _me._cursor = _me._mysql.cursor()
发表于 2019.08.13 14:50:24

前篇:关于centos 7自动同步时间的正确打开方式

后篇:centos 7开机提示selinux规则加载失败解决方案


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK