fetchone上下文管理器sqlalchemy关闭连接

时间:2017-09-27 16:40:24

标签: python sqlalchemy contextmanager

我有一个特定的用例:

  1. 我正在使用SQLAlchemy Core
  2. 我只想.fetchone()
  3. ResultsProxy
  4. 上下文管理器是否会自动关闭我的连接,还是必须手动关闭它?:
  5. ::

    with engine.connect() as sqla_conn:
        a_result_dict = dict(sqla_conn.execute(a_sqlalchemy_selectable).fetchone())
    
    # will sqla_conn be closed after __exit__? 
    # or will it be kept open because the cursor may still have available rows to fetch?
    

1 个答案:

答案 0 :(得分:0)

一旦程序超出'with'范围,上下文管理器将关闭连接,有关更多信息,请参阅link

相关问题