import MySQLdb
try:
conn = MySQLdb.connect(host=host,port=port,db=dbname,user=user,passwd=pwd)
except MySQLdb.Error, e:
try:
sqlError = "Error %d:%s" % (e.args[0], e.args[1])
except IndexError:
print "MySQL Error:%s" % str(e)
cursor=conn.cursor()
try:
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
conn.rollback()
conn.close()
except MySQLdb.Error, e:
try:
sqlError = "Error %d:%s" % (e.args[0], e.args[1])
except IndexError:
print "MySQL Error:%s" % str(e)try:
conn = MySQLdb.connect(host=host,port=port,db=dbname,user=user,passwd=pwd)
cursor=conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
conn.rollback()
conn.close()
except MySQLdb.Warning, w:
sqlWarning = "Warning:%s" % str(w)from warnings import filterwarnings filterwarnings(‘error‘, category = MySQLdb.Warning)之后便可以通过try...except..结构捕获到这个异常了。
try:
conn = MySQLdb.connect(host=host,port=port,db=dbname,user=user,passwd=pwd)
cursor=conn.cursor()
cursor.execute(sql)
result = cursor.fetchall()
cursor.close()
conn.rollback()
conn.close()
except MySQLdb.Warning, w:
sqlWarning = "Warning:%s" % str(w)
except MySQLdb.Error, e:
sqlError = "Error:%s" % str(e)原文:http://blog.csdn.net/zyz511919766/article/details/20546237