在PyMySQL库中,在cursors.py中调用以下函数:
def __enter__(self):
return self
def __exit__(self, *exc_info):
del exc_info
self.close()这意味着如果我在with语句中使用cursor类,那么每当我离开嵌套块时,游标都应该关闭。为什么它会保持不变?
db = pymysql.connect(config)
with pymysql.cursors.Cursor(db) as cursor:
print(cursor)
print(cursor)另外:
db = pymysql.connect(config)
with db.cursor() as cursor:
print(cursor)
print(cursor)这两种形式都会返回两次cursor对象打印(一次在with语句中,另一次从with语句中打印?)。我做错了什么吗?
发布于 2019-01-30 05:30:27
关闭游标并不会使游标为空,只是将其从数据库中分离出来。请尝试打印cursor.connection。
此外,我认为您期望使用"with“关键字来删除有问题的对象,但它实际上只是围绕着enter和exit函数的语法糖。
https://stackoverflow.com/questions/54429680
复制相似问题