[Tutor] Trapping warnings from MySQLdb

Tim Johnson tim at johnsons-web.com
Tue Apr 14 03:54:21 CEST 2009


On Monday 13 April 2009, Kent Johnson wrote:
<....>
> >From a quick look at MySQLdb-1.2.2, it seems to be using the python
>
> std lib module warnings to report warnings.
> http://docs.python.org/library/warnings.html
>
> >From the warnings docs, it seems like warnings.simplefilter() is what
>
> you are looking for. Try this, perhaps in your wrapper module:
> import warnings
> import MySQLdb
> warnings.simplefilter("error", MySQLdb.Warning)
>
> I think that will tell the warnings module to treat MySQLdb.Warning as an
 Yes! That is what I was looking for.
 Here's the code (in my wrapper class)
	def rcmd(self,s,test=0,trapwarnings=0):
		self._connect_method = 'row'
		if test:
			print("<BR>SQL = %s<br>" % (s))  #DEBUG st
		if trapwarnings:
			warnings.simplefilter("error", MySQLdb.Warning)
		return self._rdb.execute(s)	
		
## and the interface:
	try:
		db.rcmd(cmd,0,1)
	except _mysql_exceptions.Warning,e:
		## give user ad hoc analysis of the offending cmd
                handle_warnings_here()

## Thanks a lot Ken
tim


More information about the Tutor mailing list