如何解决Python MySQLdb:connection.close与cursor.close
使用with
,此工具可让您创建一个临时游标,一旦返回上一个缩进级别,该游标将关闭。
from contextlib import closing
with closing( connection.cursor() ) as cursor:
(indented) use the cursor
(non-indented) cursor is closed.
connection.close()
解决方法
如果我使用MySQLdb通过Python连接到MySQL-Server。我创建一个connection
和cursor
这样的:
connection = MySQLdb.connect(...)
cursor = connection.cursor()
# process
完成MySQL处理后,应关闭connection
。现在我在想:connection
通过执行以下操作是否足以关闭:
connection.close()
还是我必须先关闭cursor
然后再关闭connection
?像这样:
cursor.close()
connection.close()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。