从python脚本获取“OperationalError:1366” - MySQL 5.6。 MySQLdb 1.2.3

时间:2013-07-31 13:46:07

标签: python mysql utf-8 utf8mb4

环境:

  • 的MySQL蟒-1.2.3.win-AMD64的py2.7

  • 蟒-2.7.amd64

  • 的MySQL的安装程序-社区5.6.12.2

  • Windows Server 2008 R2数据中心

使用python脚本插入数据时,我收到操作错误1366。我需要运行2010年的EPFImporter

我认为问题在于MySQLdb只能处理uft8而不能处理utf8mb4_unicode_ci。我需要导入的数据包含utf8mb4 charackters,我可能不会删除它们。

从CMD命令行运行脚本,在成功插入~70.000行后,会出现以下错误消息:

2013-07-31 15:14:53,460 [ERROR]: Fatal error encountered while ingesting 'C:\webserver\www\site1\assets\scripts
Traceback (most recent call last):
  File "C:\webserver\www\site1\assets\scripts\EPFImporter\EPFIngester.py", line 129, in ingestFull
    self._populateTable(self.tmpTableName, skipKeyViolators=skipKeyViolators)
  File "C:\webserver\www\site1\assets\scripts\EPFImporter\EPFIngester.py", line 379, in _populateTable
    cur.execute(exStr)
  File "C:\Windows\python\lib\site-packages\MySQLdb\cursors.py", line 174, in execute
    self.errorhandler(self, exc, value)
  File "C:\Windows\python\lib\site-packages\MySQLdb\connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
OperationalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x98\\x8A A...' for column 'description' at row 1")

我已经完成了:

ALTER DATABASE databaseXYZ CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci

我将以下内容从INNODB更改为:

default storage engine  MyISAM  Documentation
default tmp storage engine  MyISAM 

在此更改之前,它根本没有导入任何行。

我还能做什么?通常它应该工作,因为在共享主机上我在MySQL服务器版本:5.5.28上运行它并且它工作正常

EDIT1 :为了测试,我安装了最新的XAMPP版本,在XAMPP下,脚本运行良好,只运行XAMPP-MySQL服务器而不是Apache。 Python和MySQLdb版本保持不变,并且未受影响。所以我现在很确定我在MySQL-Server的配置上做错了。在XAMPP中,DB与InnoDB Engine一起运行。

以下是XAMPP MYSQL服务器的规格:

Server: 127.0.0.1 via TCP/IP
Server type: MySQL
Server version: 5.6.11 - MySQL Community Server (GPL)
Protocol version: 10
User: root@localhost
Server charset: UTF-8 Unicode (utf8)

有没有人知道我可以做些什么来追踪我原来的MySQL设置中的问题?它可能是变量设置,MYSQL错误处理或Python连接器吗?

1 个答案:

答案 0 :(得分:1)

我通过比较XAMPP-MySQL和常规MySQL值解决了这个问题 phpmyadmin> localhost>变量

我在常规MySQL变量中更改了一些值,所以我不是100%确定哪个更改最终解决了问题,但我想它正在改为:

sql mode    NO_ENGINE_SUBSTITUTION

我之前还有一些像strict_mode这样的其他值,我删除了。