假设这段代码:
connection = get_some_connection() # maybe from oursql
with connection.cursor() as cursor:
cursor.execute('some query')
我理解完成后cursor.close()
会自动执行。例外情况如何?我必须把它们放在里面吗?
connection = get_some_connection() # maybe from oursql
with connection.cursor() as cursor:
try:
cursor.execute('some query')
except IntegrityError, e:
# handle exceoption
或者有没有更好的方法来使用 with 语句来处理它们?