Search code examples
pythonmysqlerror-handlingpyqtqmessagebox

Display MySQL error in QMessageBox


I'm running a local MySQL server for developing my PyQt application. It would be nice if I could display a QMessageBox if server is down, so that endusers would have some idea why is application not starting.

If I shut down the server and run my program from terminal, I get the usual response:

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (2)")

my code is simple

import pymysql as lite

con = lite.connect(host='127.0.0.1',unix_socket='/run/mysqld/mysqld.sock', user='ivica',passwd='pass',db='baza',charset='utf8')

#define one class that inherits QMainWindow and so on...

Is there a way for me to actually display a QMessageBox stating "MySQL server is down!" or something similar? If MySQL server is not running, my application window will not even show, just the terminal error.

:EDIT:

After proposed changes, my code looks like this:

con = None #this is how I make it global, eg. not in any method or class (?)

def dbconnect():
    global con
    #con = None
    try:
        if os.name == 'nt':
            con = lite.connect(host='127.0.0.1', user='ivica',passwd='pass',db='baza',charset='utf8')
        else:
            con = lite.connect(host='127.0.0.1',unix_socket='/run/mysqld/mysqld.sock', user='ivica',passwd='pass',db='baza',charset='utf8')
    except lite.err.OperationalError as err:
        msgBox = QtGui.QMessageBox()
        msgBox.setText(str(err))
        msgBox.show()
    return con

class Logon(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.ui=Ui_dlgLogovanje()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.dugmeUloguj, QtCore.SIGNAL("clicked()"), self.doLogin)

    def doLogin(self):          
        with dbconnect():
            cur = dbconnect().cursor()

and error I get is:

Traceback (most recent call last):
  File "main.py", line 59, in doLogin
    with dbconnect():
AttributeError: __exit__

:EDIT 2:

After unutbu's answer, and some of my fiddling with the code, this is the solution I was looking for:

con = None

def dbconnect():
    global con
    try:
        if os.name == 'nt': 
            con = lite.connect(host='127.0.0.1', user='ivica',passwd='pass',db='baza',charset='utf8')
        else:
            con = lite.connect(host='127.0.0.1',unix_socket='/run/mysqld/mysqld.sock', user='ivica',passwd='pass',db='baza',charset='utf8')
    except lite.err.OperationalError as err:
        msgBox = QtGui.QMessageBox()
        msgBox.setText(str(err))
        msgBox.show()
    return con

class Logon(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.ui=Ui_dlgLogovanje()
        self.ui.setupUi(self)
        QtCore.QObject.connect(self.ui.dugmeUloguj, QtCore.SIGNAL("clicked()"), self.doLogin)

    def doLogin(self):      
        if con == None:
            reply = QtGui.QMessageBox.warning(self, 'Greška',
            "Can't establish connection to database!", QtGui.QMessageBox.Ok)
            if reply == QtGui.QMessageBox.Ok:
                self.close() #and when user clicks OK program closes

        else:
        with dbconnect():
            cur = dbconnect().cursor()
                    #do other database stuff, check credentials etc.

Solution

  • Use a try...except block to handle the OperationalError exception:

    import sys
    from PyQt4 import QtGui
    import pymysql as lite
    
    
    def dbconnect():
        global con
        config = {
            'host' : '127.0.0.1',
            'user' = 'ivica',
            'passwd' = 'pass',
            'db' = 'baza',
            'charset' = 'utf8'
            }
        try:
            if os.name == 'nt':
                con = lite.connect(**config)
            else:
                con = lite.connect(unix_socket = '/run/mysqld/mysqld.sock', **config))
        except lite.err.OperationalError as err:
            msgBox = QtGui.QMessageBox()
            msgBox.setText(str(err))
            msgBox.exec_()
    
    if __name__ == '__main__':
        app = QtGui.QApplication(sys.argv)
        con = None
        dbconnect()
        if con is not None:
            sys.exit(app.exec_())
    

    It sounds like the MySQL connection is a core part of your program, so you might as well establish the connection at the very beginning or exit if the connection process is unsuccessful.

    Also, using

    with dbconnect():
        ...
    

    is not compatible with defining a global con, since the connection is closed when Python exits the with-block.