chernevik
chernevik

Reputation: 4000

Trapping MySQL Warnings In Python

I would like to catch and log MySQL warnings in Python. For example, MySQL issues a warning to standard error if you submit 'DROP DATABASE IF EXISTS database_of_armaments' when no such database exists. I would like to catch this and log it, but even in the try/else syntax the warning message still appears.

The try/except syntax does catch MySQL errors (eg, submission of a typo like 'DRP DATABASE database_of_armaments').

I have experimented with <<except.MySQLdb.Warning>> -- no luck. I've looked at the warnings module, but don't understand how to incorporate it into the try/else syntax.

To be concrete, how do I get the following (or something like it) to work.

GIVEN: database 'database_of_armaments' does not exist.

try:
    cursor.execute('DROP DATABASE IF EXISTS database_of_armaments')
except: <<WHAT DO I PUT HERE?>>
    print 'There was a MySQL warning.' 
    <<AND what goes here if I want to get and manipulate information about the warning?>>

UPDATE:

Thanks for the comments. I had tried these and they didn't work -- but I had been using a DatabaseConnection class that I wrote for a connection, and its runQuery() method to execute. When I created a connection and cursor outside the class, the try/except Exception caught the "Programming Error", and except MySQLdb.ProgrammingError worked as advertised.

So now I have to figure out what is wrong with my class coding.

Thank you for your help.

Upvotes: 15

Views: 23162

Answers (6)

Victor Gavro
Victor Gavro

Reputation: 1407

first you should turn on warnings to treat as exceptions, and only then you can catch them. see "error" warnings filter - https://docs.python.org/3/library/warnings.html#the-warnings-filter

Upvotes: 1

rafaelxy
rafaelxy

Reputation: 322

I managed to trap the mysql warning like this:

import _mysql_exceptions

try:
    foo.bar()
except _mysql_exceptions.Warning, e:
    pass

Upvotes: 2

Christian
Christian

Reputation: 3972

Plain and simple

def log_warnings(curs):
    for msg in curs.messages:
        if msg[0] == MySQLdb.Warning:
            logging.warn(msg[1])

cursor.execute('DROP DATABASE IF EXISTS database_of_armaments')
log_warnings(cursor)

msg[1] example :- (u'Warning', 1366L, u"Incorrect integer value: '\xa3' for column 'in_userid' at row 1")

Upvotes: 0

dangerouslyfacetious
dangerouslyfacetious

Reputation: 551

I think the exception you want to catch is a MySQLdb.ProgrammingError, and to get information about it, just add a variable to store the error data (a tuple) in after that i.e:

try:
    cursor.execute('DROP DATABASE IF EXISTS database_of_armaments')
except MySQLdb.ProgrammingError, e:
    print 'There was a MySQL warning.  This is the info we have about it: %s' %(e) 

Upvotes: 3

S.Lott
S.Lott

Reputation: 391952

Follow these steps.

  1. Run it with except Exception, e: print repr(e).

  2. See what exception you get.

  3. Change the Exception to the exception you actually got.

Also, remember that the exception, e, is an object. You can print dir(e), e.__class__.__name__, etc.to see what attributes it has.

Also, you can do this interactively at the >>> prompt in Python. You can then manipulate the object directly -- no guessing.

Upvotes: 15

karlcow
karlcow

Reputation: 6972

Have you tried something like this?

try:
    cursor.execute(some_statement)
except MySQLdb.IntegrityError, e: 
    # handle a specific error condition
except MySQLdb.Error, e:
    # handle a generic error condition
except MySQLdb.Warning, e:
    # handle warnings, if the cursor you're using raises them

Upvotes: 8

Related Questions