我可以在MySQLdb.Connection对象中使用with语句吗? [英] Can I use with statement with MySQLdb.Connection object?

查看:84
本文介绍了我可以在MySQLdb.Connection对象中使用with语句吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以将上下文管理器withConnection对象一起使用,并编写如下代码:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆