python - trapping a MySql warning -


in python script trap "data truncated column 'xxx'" warning durnig query using mysql.

i saw posts suggesting code below, doesn' work.

do know if specific module must imported or if option/flag should called before using code?

thanks all

afeg

import mysqldb try:     cursor.execute(some_statement)     # code steps here: no warning trapped     # code below except mysqldb.warning, e:     # handle warnings, if cursor you're using raises them except warning, e:     # handle warnings, if cursor you're using raises them 

warnings that: warnings. reported (usually) stderr, nothing else done. can't catch them exceptions because aren't being raised.

you can, however, configure do warnings, , turn them off or turn them exceptions, using warnings module. instance, warnings.filterwarnings('error', category=mysqldb.warning) turn mysqldb.warning warnings exceptions (in case caught using try/except) or 'ignore' not show them @ all. can (and should) have more fine-grained filters category.


Comments

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -