我对Python很新,下面是我正在看的一些代码:
try:
connection = getConnection(database)
cursor = connection.cursor()
cursor.execute("some query")
except:
log.error("Problem.")
raise
finally:
cursor.close()
connection.close()
正在清理吗?在我写的其他语言中,我习惯于这样做:
connection = None
cursor = None
try:
connection = getConnection(database)
cursor = connection.cursor()
cursor.execute("some query")
except:
log.error("Problem.")
raise
finally:
if cursor is not None:
cursor.close()
if connection is not None:
connection.close()