mysql的字符集

背景

python连接mysql存储数据时,如果数据中有表情符号等,可能碰到以下两类警告:

Warning: Incorrect string value: 'xF0x9FxA4x93' for column 'uname' at row 11
Warning: Invalid utf8 character string: 'F09381'

方案

要避免这类警告,保证存储数据的完整性,需要确保mysql的server(database)-connection-client通路上每一个节点的字符编码都为utf8mb4.

服务器端的设置

[mysql]
default-character-set=utf8mb4

[mysqld]
character-set-server=utf8mb4

数据库的设置,创建数据库时:

CREATE TABLE `dbbooks_versions` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ctime` datetime NOT NULL,
  PRIMARY KEY (`id`),
  KEY `ctime` (`ctime`) USING BTREE
) ENGINE=MyISAM AUTO_INCREMENT=0 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;

客户端、连接的设置

通过set names实现,从以下代码示例可以看出:set names既影响client又影响connection,set character set只影响client。

In [16]: %paste
from Utils import DBUtil
dbUtil = DBUtil("books", "lh")
dbUtil.cur.execute("SET NAMES utf8mb4")
dbUtil.cur.execute("show variables like '%colla%'")
print dbUtil.cur.fetchone()
dbUtil.cur.execute("show variables like '%charac%'")
print dbUtil.cur.fetchone()
## -- End pasted text --
(u'collation_connection', u'utf8mb4_general_ci')
(u'character_set_client', u'utf8mb4')

In [17]: %paste
from Utils import DBUtil
dbUtil = DBUtil("books", "lh")
dbUtil.cur.execute("SET CHARACTER SET utf8mb4")
dbUtil.cur.execute("show variables like '%colla%'")
print dbUtil.cur.fetchone()
dbUtil.cur.execute("show variables like '%charac%'")
print dbUtil.cur.fetchone()
## -- End pasted text --
(u'collation_connection', u'utf8_general_ci')
(u'character_set_client', u'utf8mb4')
本文原创发表于http://www.cnblogs.com/qijj,转载请保留此声明。
原文地址:https://www.cnblogs.com/qijj/p/6414488.html