python3 操作MYSQL实例及异常信息处理--用traceback模块
# 用traceback模块查看异常
import traceback
import pymysql
db = pymysql.connect(host='localhost', user='root', password='root', port=3306, db='spiders')
cursor = db.cursor()
try:
sql = 'SELECT graph, brand_name FROM base_info'
# 执行sql语句
cursor.execute(sql)
result = cursor.fetchone()
except:
# 输出异常信息
traceback.print_exc()
# 如果发生异常,则回滚
db.rollback()
finally:
# 最终关闭数据库连接
db.close()
Only action can relieve the uneasiness.