简体   繁体   中英

How to catch and display the extact error message in python

I have codes which uses python to insert data from excel into mysql.
I am trying to catch some error message and display more clear information to the end user, like:

if you are trying to insert NaN into mysql, the system show below info:
''' 1054 (42S22): Unknown column 'nan' in 'field list' '''

How can I catch this error message to display more clear info, like:

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

I use

try: 
except Exception as:
    print(e)

to catch the error message, but this can not show the explicit info.
I might be wrong, please feel free to commet it.
Thanks.

it is quite straight forwar see manual

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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM