我正在运行一个本地MySQL服务器来开发我的PyQt应用程序。如果我可以在服务器关闭的情况下显示QMessageBox会很好,这样最终用户就会知道为什么应用程序无法启动。
如果我关闭服务器并从终端运行我的程序,我得到通常的回复:
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.
答案 0 :(得分: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
- 块时连接已关闭。