Trapping MySQLdb warnings

srinivas hn hnsri49 at gmail.com
Thu Jun 16 14:52:03 EDT 2011


Hi Tim,

Use this method it will sort tour problem.

def do_query(insert_query):
   import warnings

with warnings.catch_warnings():
    warnings.simplefilter('error', MySQLdb.Warning)
    try:
      cursor.execute(insert_query)
      conn.commit()
      return 'Success'
    except MySQLdb.Error, error:
      logging.error("Error in insertion %s query is ", error)
      return 'Failure'
    finally:
      conn.close()


try:
   xyz = do_query(insert_query)
except MySQLdb.Warning, warning:
   logging.warning(warning)


you need to use the with statement and then you need to catch the warnings
hope it helps
CHEERS
CNA
9986229891


On Thu, Jun 16, 2011 at 9:25 PM, Tim Johnson <tim at johnsons-web.com> wrote:

> * Tim Johnson <tim at johnsons-web.com> [110615 18:53]:
> > * geremy condra <debatem1 at gmail.com> [110615 18:03]:
> > > On Wed, Jun 15, 2011 at 6:58 PM, Tim Johnson <tim at johnsons-web.com>
> wrote:
> > > > Using Python 2.6.5 on linux.
> > > >
> > > > When using MySQLdb I am getting warnings printed to stdout, but I
> would
> > > > like to trap, display and log those warnings.
> > <.....>
> > > Have you tried
> http://docs.python.org/library/warnings.html#temporarily-suppressing-warnings
> >   Hi Geremy:
> >   I just looked at the docs there. This is a new module (to me), and
> >   I am unsure of the implementation or whether this is what I should
> >   use.
> >   I tried the following :
> >               try :
> >                       self.__rdb.execute(S)
> >               except warnings.catch_warnings:
> >                       std.mrk('',0,"mysql.py:196")  ## DEBUG call
> > ## and it does not look like my code modification
> > ## is catching the warning.
>   Well, I am so far, absolutely baffled. I've looked at the docs for
>  the `warning' module. No real examples that I can see. and why has
>  the _mysql_exceptions.Warning exception no longer working?
>  grrr..
>  Be good to hear some other experiences here.
>  thanks
> --
> Tim
> tim at johnsons-web dot com or akwebsoft dot com
> http://www.akwebsoft.com
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110617/0fcff4ca/attachment.html>


More information about the Python-list mailing list