在python脚本中正确设置编码

时间:2018-01-25 23:04:06

标签: python mysql utf-8 character-encoding

我的MySQL数据库:

mysql> show create database response;
+----------+------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                |
+----------+------------------------------------------------------------------------------------------------+
| response | CREATE DATABASE `response` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_polish_ci */ |
+----------+------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

变量:

mysql>  SHOW variables LIKE '%character_set%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0,00 sec)

表:

mysql> show create table autoresponse_config;
| Table               | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                     |
+---------------------+--------------------------------------------------------| autoresponse_config | CREATE TABLE `autoresponse_config` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `address` varchar(255) NOT NULL,
  `enabled` tinyint(1) NOT NULL,
  `changed` datetime NOT NULL,
  `expires` datetime NOT NULL,
  `subject` varchar(255) NOT NULL,
  `message` longtext NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `address` (`address`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='Response - Autoresponse Configurations' 

示例行(请注意message列):

mysql> select * from autoresponse_config;
+----+-----------------------+---------+---------------------+---------------------+---------+---------------------------------------------------------------------------------------------------------+
| id | address               | enabled | changed             | expires             | subject | message                                                                                                 |
+----+-----------------------+---------+---------------------+---------------------+---------+---------------------------------------------------------------------------------------------------------+
|  1 | Email@example.com |       1 | 2018-01-25 20:48:19 | 2018-02-24 00:00:00 | Urlop6  | ąęśćóóŻÓŻAŁÓŁĆGĘŚLĄJAŹŃżŻ                      |
+----+-----------------------+---------+---------------------+---------------------+---------+---------------------------------------------------------------------------------------------------------+
1 row in set (0,00 sec)

问题是:

为什么在this python script中 从数据库中检索的message变量是

????óó?Ó?A?Ó??G??L?JA????

而不是:

ąęśćóóŻÓŻAŁÓŁĆGĘŚLĄJAŹŃżŻ`

如何在此脚本中正确设置字符集?

1 个答案:

答案 0 :(得分:1)

在python脚本中设置连接字符编码,如下所示:

connection = backend.connect(
            unicode=True,
            cursorclass=backend.CURSOR_DICT
            )

connection.set_character_set('utf8')

如果还不够,还要添加:

cursor = backend.open_cursor(connection)
cursor.execute('SET NAMES utf8;')
cursor.execute('SET CHARACTER SET utf8;')
cursor.execute('SET character_set_connection=utf8;')