【发布时间】:2014-01-23 18:59:57
【问题描述】:
我是 python 新手,如果这个问题很愚蠢,很抱歉,但是有人可以告诉我这里发生了什么。
当我在 mdb.connect() 调用中运行以下代码且没有错误时,代码运行良好。
但是当我故意插入错误时(例如,放入'localhostblahblah'),我在执行时收到'NameError: name 'con' is not defined'错误。
我认为在 try 子句中定义的变量应该可以在 finally 子句中访问。怎么回事?
#!/usr/bin/python
import MySQLdb as mdb
import sys
try:
con = mdb.connect('localhost','jmtoung','','ptb_genetics')
except mdb.Error, e:
print "Error"
sys.exit(1)
finally:
if con:
con.close()
【问题讨论】:
-
您希望发生什么?
标签: python mysql-python