Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7769

Re: Trapping MySQLdb warnings

Date 2011-06-16 13:56 -0800
From Tim Johnson <tim@johnsons-web.com>
Subject Re: Trapping MySQLdb warnings
References <20110616015837.GA1885@johnsons-web.com> <BANLkTint8YP9W7hUnvYh4nmMo_6h0WPXTA@mail.gmail.com> <20110616023829.GB1885@johnsons-web.com> <20110616155533.GD1885@johnsons-web.com> <BANLkTikjEtmRtnqQnnCHUkc1DL7QuRKm9g@mail.gmail.com>
Organization AkWebsoft
Newsgroups comp.lang.python
Message-ID <mailman.43.1308261380.1164.python-list@python.org> (permalink)

Show all headers | View raw


* srinivas hn <hnsri49@gmail.com> [110616 11:06]:
> Hi Tim,
> 
>    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)
  Hi Again srinavas:
  For an overview, I need to interate thru a file, executing each
  line as an insert statement. Based on your valuable input, I have
  the following:
## ------------------------------------------------
def __process_line(self):
  """For each line in the file .."""
  try :
    self.db.simple(self.read())
  except MySQLdb.Warning,e:
	## catching warnings here, log, count, abort
    ## or some other action based on implementation
    print(e) ## and count, log .... etc	
## ------------------------------------------------
def simple(self,insert_query):
  """Executing one line.""" 
  cursor = self.__rdb 
  with warnings.catch_warnings():
    warnings.simplefilter('error', MySQLdb.Warning)
    cursor.execute(insert_query)

## This works well, but I'd like to do something further: Control is
transfered back to the calling function's `except' block when the
first warning is found. There can be and are multiple warning
conditions on each line. Is there a way to trap all of them?
I am unclear from the docs that I am reading at
http://docs.python.org/library/warnings.html
whether that can be done.
Thanks again
-- 
Tim 
tim at johnsons-web dot com or akwebsoft dot com
http://www.akwebsoft.com

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: Trapping MySQLdb warnings Tim Johnson <tim@johnsons-web.com> - 2011-06-16 13:56 -0800

csiph-web