Note that there are some explanatory texts on larger screens.

plurals
  1. PODisplay MySQL error in QMessageBox
    primarykey
    data
    text
    <p>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.</p> <p>If I shut down the server and run my program from terminal, I get the usual response:</p> <p><code>pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on '127.0.0.1' (2)")</code></p> <p>my code is simple</p> <pre><code>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... </code></pre> <p>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.</p> <p>:EDIT:</p> <p>After proposed changes, my code looks like this:</p> <pre><code>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() </code></pre> <p>and error I get is:</p> <pre><code>Traceback (most recent call last): File "main.py", line 59, in doLogin with dbconnect(): AttributeError: __exit__ </code></pre> <p>:EDIT 2:</p> <p>After unutbu's answer, and some of my fiddling with the code, this is the solution I was looking for:</p> <pre><code>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. </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload