Menu

Catching OperationalError not working?!

Help
2007-03-03
2012-09-19
  • David Babcock

    David Babcock - 2007-03-03

    When logging in to Mysql thru my program app, trying to catch most common errors that might occur so user gets better feedback as why they got an error.
    Not having any problem catching most of the ones I want like this: (not catching 1046 error though, have to use except IndexError to catch error.)

    -------------------------(from app)
    except MySQLdb.Error, e:
    print "MySQLdb.Error is:"
    print "Error %d: %s" % (e.args[0], e.args[1])
    # put above back to get error numbers. or for testing.
    if (e.args[0] == 2002): #catch if mysql not running. WORKS
    print "Connection Error: MySQL is not running on localhost. Please check."
    self.exists = "nomysql"
    if (e.args[0] == 1049): #never seem to see this one.
    print "Database %s doesn't Exist. Need to Create it." % (db)
    self.exists = "nodb"
    if (e.args[0] == 1046): # No Database Selected. Not working!
    print "No Database Doesn't Exist"
    self.exists = "nodbname"
    if (e.args[0] == 1045): # catch if user password wrong. WORKS
    print "Access Denied. Possibly wrong Owner/Password."
    self.exists = "nouser" #this works now 8/30/06

    except IndexError: #catch if there is no database by self.db name or stockinfo empty. WORKS
    print "Mysql IndexError: Pyfolio doesn't exist, but user logged in. Need to Create it."
    self.exists = "nodb"


    So if delete the database and try to login, user can login, but there isn't a database by the name given. Want to catch that better, but except MySQLdb.Error exception isn't catching it, tried doing and 'except OperationalError' and that won't work at all. This is the output of user/password good, but not database by that name.:
    ---------------------------(output from app with no database)
    Mysql IndexError: Pyfolio doesn't exist, but user logged in. Need to Create it."

    Traceback (most recent call last):
    File "/usr/lib64/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/_core.py", line 13535, in <lambda>
    lambda event: event.callable(event.args, *event.kw) )
    File "main.py", line 114, in ckuser
    self.connect(self)
    File "main.py", line 161, in connect
    self.online = self.db.ckyahoo(self.owner) #check if online
    File "/home/decibels/Pyfolio/stockdb.py", line 129, in ckyahoo
    c.execute(sql)
    File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 137, in execute
    self.errorhandler(self, exc, value)
    File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
    raise errorclass, errorvalue
    _mysql_exceptions.OperationalError: (1046, 'No database selected')


    Tried also catching it by adding another exception:
    -------------------------- (doesn't seem to work)
    except MySQLdb.OperationalError:
    print "MySQLdb.OperationalError is:"
    print "Error %d: %s" % (e.args[0], e.args[1])
    if (e.args[0] == 1046): # No Database Selected
    print "No Database Selected"
    self.exists = "nodbname"
    -------------------------
    But the above produces the same exact output as without it.

    ?? So why does they other errors like: 1045 and 2002 work, but can't catch the 1046 error except with <b>IndexError</b>?
    Maybe I should be happy that I can catch it and code works, just would like to get it working the way I expect. Any clues?

     
    • Andy Dustman

      Andy Dustman - 2007-03-03

      I have no idea why you would get IndexError unless it is something being generated by your own code. Since there's no traceback for that exception, there's no way of knowing.

      Also keep in mind that OperationalError is a subclass of Error, so you have to have the except OperationalError clause before the except Error clause, because except Error will catch OperationalError too.

       
      • David Babcock

        David Babcock - 2007-03-04

        Well, I'm not sure what is going on either cause putting the OperationalError before the Error exception didn't seem to make a hill of beans.
        As far as my code, if I knew the fact everything had shifted left when submitted I might have made it look better.

        But as far as make the code more readable by using the MySQLdb.constants.ER. When I first started using MySQLdb to write this App, didn't
        find any examples about that on the net so used what was available. Even when searched for this didn't find much, maybe need to change the search.

        I did quickly try 'from MySQLdb.constants.ER import * ' which hadn't thought of before and that did seem to catch it with OperationalError in the
        exception, but not exactly the way I expected, cause first saw the exception caught when typed in the wrong password and was expecting it to go to the
        Error exception, but probably the way I have the code written right now. So I will mess with it some more this weekend. Maybe I can find some better
        examples of using it somewhere.

        Except for these exceptions, I have had lot of success using MySQLdb with my App and found it easy to use. Thanks!

         
    • Andy Dustman

      Andy Dustman - 2007-03-04

      I'll also point out that MYSQLdb.constants.ER has all the error numbers defined symbolically, and using these would make your code somewhat easier to read.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.