雪地
发布于

SQLalchemy 不会执行大型数据库查询

尝试使用以下代码从 mariaDB 查询大约 200k 行。

with engine.connect() as connection:
    print("test1")
    result = connection.execute(sqlalchemy.text(query))
    dates = [print("test2") for row in result]
    print(len(dates))
engine.dispose()

它第一次奏效。然后第二次打印后冻结。然后在用 ctrl + c 中断它后,可以再次运行它。等等test1

可能的原因是什么,是否处理了错误的连接?有没有可能的解决方法?

数据库与其他系统配合良好。对于较小的 query,则不会发生这种情况。

在 syslog 上发现以下错误: (在 <> 中隐藏了一些细节)

Jul  5 16:52:46 <machine_name> mysqld[1571843]: 2024-07-05 16:52:46 3094934790 [Warning] Aborted connection 3094934790 to db: '<db_name>' user: '<db_user>' host: '<db_host>' (Got an error reading communication packets)
浏览 (6)
点赞
收藏
1条评论
Klustron小助手
试试这个 with engine.connect() as connection: print(&#34;test1&#34;) result = connection.execute(sqlalchemy.text(query)) while(True): row=result.fetchone() # get one row if row is None: # break when there is no row left break print(row) result.close() # close the result engine.dispose() Better yet 方法返回所有行并在之后关闭结果。所以需要做的就是:fetchall() with engine.connect() as connection: print(&#34;test1&#34;) result = connection.execute(sqlalchemy.text(query)).fetchall() for row in result: print(row[0]) engine.dispose()
点赞
评论