Getting error condition from MySQLdb

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Derek Fountain

    Getting error condition from MySQLdb

    I was trying to use MySQLdb to connect to a database. All is OK, except I
    can't figure out how to get the details of an error. Suppose I try to
    connect to a non existant server, or with the wrong password - how do I get
    a meaningful error message which I can present to my user?

  • Andy Todd

    #2
    Re: Getting error condition from MySQLdb

    Derek Fountain wrote:
    [color=blue]
    > I was trying to use MySQLdb to connect to a database. All is OK, except I
    > can't figure out how to get the details of an error. Suppose I try to
    > connect to a non existant server, or with the wrong password - how do I get
    > a meaningful error message which I can present to my user?
    >[/color]

    In Python whenever an error occurs an exception is raised. I'd suggest a
    quick read of the section of the tutorial that explains this;

    The official home of the Python Programming Language


    Then fire up a Python command prompt and try some things out. For
    instance, on my Windows machine if I try;
    [color=blue][color=green][color=darkred]
    >>> import MySQLdb
    >>> mydb=MySQLdb.co nnect(db='wibbl e')[/color][/color][/color]

    I get a nice traceback;

    Traceback (most recent call last):
    File "<input>", line 1, in ?
    File "C:\PROGRA~2\PY THON23\Lib\site-packages\MySQLd b\__init__.py",
    line 63, in Connect
    return apply(Connectio n, args, kwargs)
    File "C:\PROGRA~2\PY THON23\Lib\site-packages\MySQLd b\connections.p y",
    line 115, in __init__
    self._make_conn ection(args, kwargs2)
    File "C:\PROGRA~2\PY THON23\Lib\site-packages\MySQLd b\connections.p y",
    line 41, in _make_connectio n
    apply(super(Con nectionBase, self).__init__, args, kwargs)
    OperationalErro r: (2003, "Can't connect to MySQL server on 'localhost'
    (10061)")

    If I actually start up MySQL, I get;

    Traceback (most recent call last):
    File "<input>", line 1, in ?
    File "C:\PROGRA~2\PY THON23\Lib\site-packages\MySQLd b\__init__.py",
    line 63, in Connect
    return apply(Connectio n, args, kwargs)
    File "C:\PROGRA~2\PY THON23\Lib\site-packages\MySQLd b\connections.p y",
    line 115, in __init__
    self._make_conn ection(args, kwargs2)
    File "C:\PROGRA~2\PY THON23\Lib\site-packages\MySQLd b\connections.p y",
    line 41, in _make_connectio n
    apply(super(Con nectionBase, self).__init__, args, kwargs)
    OperationalErro r: (1049, "Unknown database 'wibble'")

    Note that MySQLdb, like the majority of distributed modules, defines its
    own exceptions. MySQLdb, to the best of my knowledge, implements the
    standard DB-API exceptions. They are detailed here;



    Regards,
    Andy
    --
    --------------------------------------------------------------------------------
    From the desk of Andrew J Todd esq - http://www.halfcooked.com/



    Comment

    Working...