python - 大量行的 MySQLdb 错误的 executemany

标签 python mysql executemany

我目前正在运行一个脚本,使用 execute many 函数将值(元组列表)插入 MySQL 数据库。当我使用少量行 (`1000) 时,脚本运行良好。

当我使用大约 40,000 行时,我收到以下错误:

cursor.executemany( stmt, trans_frame)
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2538, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-1-66b44e71cf5a>", line 1, in <module>
    cursor.executemany( stmt, trans_frame)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 253, in executemany
    r = self._query('\n'.join([query[:p], ',\n'.join(q), query[e:]]))
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 346, in _query
    rowcount = self._do_query(q)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 310, in _do_query
    db.query(q)
OperationalError: (2006, 'MySQL server has gone away')

有什么建议吗?

最佳答案

sql ='SET GLOBAL max_allowed_packet=500*1024*1024'
cursor.execute(sql)

关于python - 大量行的 MySQLdb 错误的 executemany,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13291700/

相关文章:

python - 并排打印放大的 ASCII 字母图案

Python:用随机选择替换多个单词

python - 如何选择以值结尾的 3D numpy 数组的子集?

MySQL UPDATE 与同一张表的 SUBQUERY

python - Python SKLearn Logistic 回归中的虚拟变量

MySQL 根据匹配和不匹配条件更新重复 ID 全部在一张表中

MYSQL select查询,同一列必须存在多个条件

Python + sqlite3 : executemany with "constructed" values

python - 如何通过 executemany() 语句转换 pandas 数据框以进行插入?

Python MySQLdb 将双引号更改为单引号