我可以在MySQLdb.Connection对象中使用with语句吗? [英] Can I use with statement with MySQLdb.Connection object?
本文介绍了我可以在MySQLdb.Connection对象中使用with语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否可以将上下文管理器with
与Connection
对象一起使用,并编写如下代码:
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()
在块之后,像file
对象一样,是否会自动关闭conn
对象?
Will the conn
object be closed automatically after the block like with a file
object ?
谢谢.
推荐答案
MySQLdb不支持上下文管理器协议. 滚动自己的滚动条,或使用oursql.
MySQLdb does not support the context manager protocol. Roll your own, or use oursql instead.
这篇关于我可以在MySQLdb.Connection对象中使用with语句吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文