上下文经理与psycopg2连接到邮政
使用这样的上下文管理器语句进行连接很有用:
with psycopg2.connect(**dns) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
...
我应该如何定义一个类别使用上下文管理器与数据库合作的类?
class DBConn:
def __init__(self, dns):
self.conn = psycopg2.connect(**dns)
self.curs = self.conn.cursor()
def __enter__(self):
pass
def __exit__(self):
pass
It is useful to connect using a context manager statements like this:
with psycopg2.connect(**dns) as conn:
with conn.cursor(cursor_factory=psycopg2.extras.RealDictCursor) as curs:
...
How should I define a class to work with database using context managers?
class DBConn:
def __init__(self, dns):
self.conn = psycopg2.connect(**dns)
self.curs = self.conn.cursor()
def __enter__(self):
pass
def __exit__(self):
pass
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
知道了...
got it...