我正在尝试将 Pandas 数据帧写入 MySQL 数据库,并使用了以下代码:
engine = sqlalchemy.create_engine("mysql+pymysql://root:password@localhost/skills?charset=utf8mb4")
connection = engine.connect
dataframe.head().to_sql('indeed_resumes', engine, flavor='mysql', if_exists='replace',index=True)
但是,我收到以下错误:
InternalError: (1366, "Incorrect string value: '\\xE1\\xBB\\x99i\\x0AO...' for column 'work' at row 5")
MySQL表的数据类型如下:
mysql> desc indeed_resumes;
+-----------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+------------+------+-----+---------+-------+
| index | bigint(20) | YES | MUL | NULL | |
| certs | text | YES | | NULL | |
| contact | text | YES | | NULL | |
| education | text | YES | | NULL | |
| headline | text | YES | | NULL | |
| info | text | YES | | NULL | |
| skills | text | YES | | NULL | |
| summary | text | YES | | NULL | |
| updated | text | YES | | NULL | |
| work | text | YES | | NULL | |
+-----------+------------+------+-----+---------+-------+
10 rows in set (0.00 sec)
我的数据由非常长的字符串组成(有时大约 3000 个字符),因此这可能会导致错误。有什么建议么?
我似乎已经解决了这个问题。看来我还需要使用以下命令更改数据库编码。
ALTER DATABASE skills CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
ALTER TABLE indeed_resumes CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
From https://mathiasbynens.be/notes/mysql-utf8mb4 https://mathiasbynens.be/notes/mysql-utf8mb4:
“事实证明,MySQL 的 utf8 字符集仅部分实现了正确的 UTF-8 编码。它只能存储由 1 到 3 个字节组成的 UTF-8 编码符号;不支持占用 4 个字节的编码符号。
幸运的是,MySQL 5.5.3(2010 年初发布)引入了一种名为 utf8mb4 的新编码,它映射到正确的 UTF-8,从而完全支持 Unicode,包括星体符号。”
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)