Can I use with statement with MySQLdb.Connection object?

坚强是说给别人听的谎言 提交于 2019-12-19 18:29:12

问题


I wonder if I can use the context manager with with the Connection object, and write code like this:

with MySQLdb.connect(...) as conn:
    do_something()

Will the conn object be closed automatically after the block like with a file object ?

Thanks.


回答1:


MySQLdb does not support the context manager protocol. Roll your own, or use oursql instead.



来源:https://stackoverflow.com/questions/11751703/can-i-use-with-statement-with-mysqldb-connection-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!