【发布时间】:2018-06-02 10:48:15
【问题描述】:
我正在逐行读取文件,将我需要的数据存储在变量中,然后在插入语句中将它们传递以将它们逐行存储在我的数据库中。当程序通过以下异常点击 unicode 表情 mysql 时:
Traceback (most recent call last):
File "import_json.py", line 73, in <module>
crs.execute(ins_statement,parameters)
File "/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 250, in execute
self.errorhandler(self, exc, value)
File "/venv/lib/python3.4/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
raise errorvalue
File "/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 247, in execute
res = self._query(query)
File "/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 411, in _query
rowcount = self._do_query(q)
File "/venv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 374, in _do_query
db.query(q)
File "/venv/lib/python3.4/site-packages/MySQLdb/connections.py", line 277, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1366, "Incorrect string value: '\\xF0\\x9F\\x87\\xB5\\xF0\\x9F...' for column 'primary_geo' at row 1")
到目前为止,我遇到了几个编码问题,我通过将 mysqls 编码设置为 utf8mb4 解决了这些问题,如下所示:
+--------------------------+--------------------+
| Variable_name | Value |
+--------------------------+--------------------+
| character_set_client | utf8mb4 |
| character_set_connection | utf8mb4 |
| character_set_database | utf8mb4 |
| character_set_filesystem | binary |
| character_set_results | utf8mb4 |
| character_set_server | utf8mb4 |
| character_set_system | utf8 |
| collation_connection | utf8mb4_unicode_ci |
| collation_database | utf8mb4_general_ci |
| collation_server | utf8mb4_unicode_ci |
+--------------------------+--------------------+
并且还添加到我的代码中:
conn.set_character_set('utf8')
cursor.execute('SET NAMES utf8;')
cursor.execute('SET CHARACTER SET utf8;')
cursor.execute('SET character_set_connection=utf8;')
然而,像 unicode 表情符号这样的东西出现了,谁知道在导入和解析数百万行时还会发生什么。所以我想允许
cursor.execute(insert_stmnt) 添加所有正确的行并忽略错误的行,因为我只需要存储原始数据的样本。
我试过了:
` try:
crs.execute(insert_stmnt,args)
except (MySQLdb.Error):
continue`
但这只是完成程序而不向我的数据库写入任何内容。
【问题讨论】:
标签: python mysql python-3.x mysql-python