1

我有使用 python 将数据从 excel 插入 mysql 的代码。
我正在尝试捕获一些错误消息并向最终用户显示更清晰的信息,例如:

如果您尝试将 NaN 插入 mysql,系统会显示以下信息:
''' 1054 (42S22): Unknown column 'nan' in 'field list' '''

如何捕获此错误消息以显示更清晰的信息,例如:

if ErrorCode='1054':
    print('please check at original file at see whether there is empty value at certain area"

我用

try: 
except Exception as:
    print(e)

捕获错误消息,但这不能显示显式信息。
我可能是错的,请随时评论它。
谢谢。

4

1 回答 1

2

这很直接,请参阅手册

except mysql.connector.Error as err:
  if err.errno == 1054:
    print("please check at original file at see whether there is empty value at certain area")
  else:
    print("Another error")
于 2020-06-27T12:59:21.637 回答