2012-09-14 21 views
0

我正在运行本地MySQL服务器来开发我的PyQt应用程序。如果服务器关闭时可以显示QMessageBox会很好,所以最终用户会有一些想法,为什么应用程序不能启动。在QMessageBox中显示MySQL错误

如果我关闭服务器和终端运行我的程序,我得到的通常的反应:

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

我的代码很简单

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... 

有没有办法对我来说,实际上显示一个QMessageBox,声明“MySQL服务器已关闭!”或类似的东西?如果MySQL服务器没有运行,我的应用程序窗口甚至不会显示,只是终端错误。

:编辑:

修改建议后,我的代码看起来是这样的:

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() 

和错误我得到的是:

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

:编辑2:

后unutbu的回答,以及我的一些代码的摆弄,这是我正在寻找的解决方案:

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. 
+1

只有当'lite.connect'失败时才得到这个'AttributeError',或者即使建立了成功的连接,你也能得到它吗? – unutbu

+0

如果没有运行MySQL服务器,那是我得到的错误。如果服务器运行一切都很好,没有错误。 – ivica

+1

连接失败时,'con'等于'None',它没有'__exit__'方法。这就是为什么'with dbconnect()'失败。如果连接失败,您希望发生什么? – unutbu

回答

1

使用try...except块来处理OperationalError例外:

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_()) 

这听起来像MySQL连接是你的程序的核心部分,所以你还不如建立在连接如果连接过程不成功,则是开始或退出。

此外,使用

with dbconnect(): 
    ... 

不与定义一个全局con,由于当的Python离开with嵌段连接被关闭兼容。

+0

我明白了,很好的方法。另一件事困扰我;在我的代码中的某处,我使用'with con:cur = con.cursor()'和你的代码,我得到错误'NameError:全局名''con'未定义。有什么建议?谢谢,一旦我的终端出现这个错误,我会尽快接受你的回答。 – ivica

+1

用我定义它的方式,'con'是'main'中的局部变量,而不是全局变量。您需要将其定义为全局变量,或者将'con'作为参数传递给使用它的函数,或者将dbconnect()用作con:'在需要它的地方使用'。 – unutbu

+0

我编辑了一个新问题的问题。 – ivica