You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

容器化Python脚本执行SQL触发StopIteration错误如何处理?

Python多SQL执行跨环境报错问题修复

现象

编写了按顺序加载执行一系列SQL文件的Python脚本,本地运行无异常,部署到容器中执行报错。

问题代码

iterator = db_cursor.execute(sql_file.read(), multi=True)
for res in iterator:
    logger.debug("executing query: [%s], affected %d rows", res, res.rowcount)
iterator.close()

报错栈

Traceback (most recent call last):
  File "/usr/local/lib/python3.9/dist-packages/mysql/connector/cursor.py", line 486, in _execute_iter
    result = next(query_iter)
StopIteration

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/migration.py", line 91, in <module>
    for res in iterator:
RuntimeError: generator raised StopIteration

根因及修复

经排查为依赖库不一致导致的bug:
本地环境安装的MySQL驱动为mysql-connector-python,容器环境安装的为mysql-connector,后者的多语句执行迭代器存在未正确处理StopIteration的已知问题。

  • 修复方案:将两端环境的依赖包统一,建议统一使用官方维护的mysql-connector-python即可解决问题。

内容的提问来源于stack exchange,提问作者Alekc

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.24 04:36:05