Python MySQLdb,完成插入查询执行,忽略错误的插入

时间:2018-06-02 10:48:16

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

我正在逐行读取文件,将我需要的数据存储在变量中,然后在insert语句中传递它们以逐行存储在我的db中。当程序通过以下异常点击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`

但这只是完成了程序而没有向我的数据库写任何内容。

1 个答案:

答案 0 :(得分:0)

当你睡眠不足时永远不要工作。特别是当你是像我这样的新手时。我没有承诺。

try:
      crs.execute(insert_stmnt,args)
      conn.commit() 
  except (MySQLdb.Error):
      continue`