Python MySQLdb: connection.close() VS. cursor.close()
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: If I use MySQLdb to connect to MySQL-Server through python. I create a connection and a cursor like this connection = MySQLdb.connect(...) cursor = connection.cursor() # process When the MySQL-processing is done one should close the connection. Now I was wondering: Is it sufficient to close the connection bydoing connection.close() or do I have to close the cursor first and then the connection? Like this: cursor.close() connection.close() 回答1: Think less. Use tools more. from contextlib import closing with closing( connection.cursor() ) as