简体   繁体   English

带语句和SQLalchemy引擎

[英]With statement and SQLalchemy engine

When I try to implement SQLalchemy engine using following construction 当我尝试使用以下构造实现SQLalchemy引擎时

with sqlalchemy.create_engine("sqlite:///my_db.sqlite") as engine:
     (do something)

I get an error: AttributeError: __exit__ 我收到一个错误: AttributeError: __exit__

What is wrong? 怎么了? And how can I explicitly close my SQLalchemy engine? 以及如何显式关闭我的SQLalchemy引擎?

A SQLAlchemy engine is not a context manager, so it can't be used in a with statement. SQLAlchemy引擎不是上下文管理器,因此不能在with语句中使用。 It manages the connections in a pool for you, see the documentation on engine.execute() and engine.dispose() for an explanation of how. 它为您管理池中的连接,有关如何解释的信息,请参阅engine.execute()engine.dispose()上的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM