trapping a MySql warning

前端 未结 7 1044
逝去的感伤
逝去的感伤 2020-11-29 23:59

In my python script I would like to trap a \"Data truncated for column \'xxx\'\" warning durnig my query using MySql.

I saw some posts suggesting the code below, but

7条回答
  •  暖寄归人
    2020-11-30 00:35

    Raise MySQL Warnings as errors:

    import warnings, MySQLdb
    warnings.filterwarnings('error', category=MySQLdb.Warning)
    

    To ignore instead of raising an error, replace "error" with "ignore".

    Handle them in a try-except block like:

    try:
        # a MySQL DB operation that raises a warning
        # for example: a data truncated warning
    except Warning as a_warning:
        # do something here
    

提交回复
热议问题