On Mon, Apr 13, 2009 at 7:22 PM, Tim Johnson <t...@johnsons-web.com> wrote:
> FYI: Using python 2.5 with the MySQLdb module.
> I need to be able to raise an exeception on a MySQL warning.
>
> I have used my own class for years that wraps the MySQLdb module,
> but never did build in a feature that would let me 'tell' my class
> instances to raise an except for a warning:
> Example:
> I'm converting csv data into Mysql commands and executing them via
> a cursor object. I instantiate a cursor that returns a tuple as:
> self._rdb = self._conn.cursor() ## where self._conn is an object of the
>                                             ## MySQdb.connect class
> # And I instantiate a cursor that returns a dictionary with
> self._rdb = self._conn.cursor(MySQLdb.cursors.DictCursor)
>
> Is there a way to create a cursor that _will_ raise an exeception on a
> warning?
>
> I have looked at the cursor.py module and was not able to find such
> a feature. Any and all ideas would be appreciated. This will be very
> useful to us.

>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 error.

Kent
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to