【问题标题】:Python 3 + Mysql: Incorrect string value '\xF0\x9F\x85\x97\xF0\x9F...'Python 3 + Mysql:不正确的字符串值'\xF0\x9F\x85\x97\xF0\x9F...'
【发布时间】:2020-02-20 11:22:48
【问题描述】:

我在堆栈上找到了关于“不正确的字符串值”的其他问题/答案,但没有一个答案有效,所以我的情况可能有所不同。

try:
    self.cnx = mysql.connector.connect(host='localhost', user='emails', password='***',
                                               database='extractor', raise_on_warnings=True)
except mysql.connector.Error as err:
    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")
    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")
    else:
        print(err)
self.sql = self.cnx.cursor()

biography = str(row[8])
self.sql.execute("""insert into emails (biography)
                            values(%s)""",
                                         (biography,))

其中biographyutf8mb4_general_ci TEXT 列:

< Living the ???????????????? ???????????????? > Azofra & Clifford Travel Food Fashion

我明白了:

mysql.connector.errors.DataError: 1366 (22007): Incorrect string value: '\xF0\x9F\x85\x97\xF0\x9F...' for column `extractor`.`emails`.`biography` at row 1

show create table emails的输出:

show create table emails;
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table  | Create Table                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| emails | CREATE TABLE `emails` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ufeff_user_id` varchar(20) COLLATE utf8_bin DEFAULT NULL,
  `username` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `full_name` varchar(100) COLLATE utf8_bin DEFAULT NULL,
  `is_private` tinyint(1) DEFAULT NULL,
  `follower_count` int(10) DEFAULT NULL,
  `following_count` int(10) DEFAULT NULL,
  `media_count` int(10) DEFAULT NULL,
  `biography` text CHARACTER SET utf8mb4 DEFAULT NULL,
  `has_profile_pic` tinyint(1) DEFAULT NULL,
  `external_url` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `public_email` varchar(320) CHARACTER SET utf8 NOT NULL,
  `contact_phone_number` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `address_street` varchar(255) COLLATE utf8_bin DEFAULT NULL,
  `is_business` tinyint(1) DEFAULT NULL,
  `engagement` int(10) DEFAULT NULL,
  `recent_post_date` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `category` varchar(75) COLLATE utf8_bin DEFAULT NULL,
  `avg_likes` int(10) DEFAULT NULL,
  `avg_comments` int(10) DEFAULT NULL,
  `business_join_date` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `business_count` int(5) DEFAULT NULL,
  `business_ads` tinyint(1) DEFAULT NULL,
  `country_code` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `emailscol` varchar(45) COLLATE utf8_bin DEFAULT NULL,
  `city_name` varchar(75) CHARACTER SET utf8 DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`public_email`)
) ENGINE=InnoDB AUTO_INCREMENT=139 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
+--------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

【问题讨论】:

  • '< Living the ???????????????? ???????????????? > Azofra & Clifford Travel Food Fashion'
  • 我们来看看python中的连接参数。
  • 问题已更新。

标签: python mysql python-3.x character-encoding


【解决方案1】:

我假设列emails.biographyVARCHAR 类型,并且 emailsCHARSETutf8mb4。如果没有,您需要执行:

 ALTER TABLE `emails` CONVERT TO CHARACTER SET utf8mb4;

然后,如果这不能解决问题,请尝试在 Python 中创建 MySQL 游标后直接执行以下操作(假设 self.sql 是您的游标):

 self.sql.execute('SET NAMES utf8mb4;')
 self.sql.execute('SET CHARACTER SET utf8mb4;')
 self.sql.execute('SET character_set_connection=utf8mb4;')

如果这不起作用,请尝试在 Python 中创建 MySQL 连接后立即设置字符集,例如:

 self.connection.set_character_set('utf8mb4')

如果此时您仍然不走运,我们可以进一步调试 :)

更新:

试试:

ALTER TABLE `emails` CONVERT TO CHARACTER SET utf8;
ALTER TABLE `emails` CHANGE COLUMN `biography` TEXT CHARACTER SET 'utf8';

请注意,utf8mb4_general_ci 是表的排序规则,而不是编码。理想情况下,您应该使用COLLATE utf8_unicode_ci

【讨论】:

  • 它已经是 TEXT 类型的 uft8bm4 字段。问题已更新。
  • self.cnx.set_character_set('utf8mb4') 产生:AttributeError: 'MySQLConnection' object has no attribute 'set_character_set' 所以我必须这样做:self.cnx.set_charset_collation('utf8bm4') 产生:Character set 'utf8bm4' unsupported.。我也尝试了utf8bm4_general_ci,结果相同。
  • 尝试将所有utf8mb4 更改为utf8utf8mb4 是在 MySQL 5.5 版中引入的,因此您可能使用的是相当旧的 MySQL 安装。另外,我已经更新了我的答案。
  • 你能粘贴你的my.cnf吗?如果您还没有,您可能应该更新它。见这里:stackoverflow.com/a/20429481/2178980。如果你把你目前拥有的东西发给我,我可以为你做。
  • 太棒了!这拯救了我的一天。我一直在尝试使用 Python 将 Emojis 放入 MySQL,但没有成功。第二个解决方案(执行SET names utf8mb4 和其他查询)对我很有效。顺便说一句,这应该是公认的答案。我不知道为什么不是。
猜你喜欢
  • 2013-12-23
  • 2012-11-19
  • 1970-01-01
  • 1970-01-01
  • 2020-11-13
  • 2019-05-27
  • 2018-05-05
  • 2017-03-26
相关资源
最近更新 更多