pyqt5_站点管理_db_windows

from PyQt5.QtWidgets import QWidget,QLabel,QLineEdit,QGroupBox,QGridLayout,QVBoxLayout,QPushButton,QMessageBox
import pickle,pymssql,os,threading,time
#定义数据库窗口
class db_window(QWidget):
    def __init__(self):
        super().__init__()
        self.db_server = QLineEdit()
        self.db_port = QLineEdit()
        self.db_user = QLineEdit()
        self.db_password = QLineEdit()
        self.dir_db = {}
        #初始化数据库链接信息
        self.db_test_flag = -1
        self.dir_test_db = {}
        self.initUI()
        self.initDB()
    def initUI(self):
        #初始化页面
        db_server_label = QLabel('数据库地址:')
        db_port_label = QLabel('端口:')
        db_user_label = QLabel('数据库账号:')
        db_password_label = QLabel('数据库密码:')
        self.db_password.setEchoMode(QLineEdit.Password)
        db_test_button = QPushButton('测试数据库链接')
        db_input_button = QPushButton('保存数据库链接')
        db_test_button.clicked.connect(self.test_db)
        db_input_button.clicked.connect(self.save_db)
        db_dbinfo_group = QGroupBox('MSSQL数据库')
        db_mssql_grid = QGridLayout()
        db_mssql_grid.addWidget(db_server_label,0,0)
        db_mssql_grid.addWidget(db_port_label,0,1)
        db_mssql_grid.addWidget(db_user_label,0,2)
        db_mssql_grid.addWidget(db_password_label,0,3)
        db_mssql_grid.addWidget(db_test_button,0,4)
        db_mssql_grid.addWidget(self.db_server,1,0)
        db_mssql_grid.addWidget(self.db_port,1,1)
        db_mssql_grid.addWidget(self.db_user,1,2)
        db_mssql_grid.addWidget(self.db_password,1,3)
        db_mssql_grid.addWidget(db_input_button,1,4)
        db_dbinfo_group.setLayout(db_mssql_grid)

        vbox = QVBoxLayout()
        vbox.addWidget(db_dbinfo_group)
        self.test_label = QLabel('')
        vbox.addWidget(self.test_label)
        vbox.setStretchFactor(db_dbinfo_group,3)
        vbox.setStretchFactor(self.test_label,7)
        self.setLayout(vbox)
    #初始化数据库链接信息
    def initDB(self):
        try:
            if os.path.exists('.//DATA//DBinfo.pik'):
                with open('.//DATA//DBinfo.pik','rb') as db_file:
                    self.dir_db = pickle.load(db_file)
                for line in self.dir_db.keys():
                    if line == 'server':
                         self.db_server.setText(self.dir_db['server'])
                    elif line == 'port':
                        self.db_port.setText(str(self.dir_db['port']))
                    elif line == 'user':
                        self.db_user.setText(self.dir_db['user'])
                    elif line == 'password':
                        self.db_password.setText(self.dir_db['password'])
        except Exception as error:
            return
    #数据库测试按钮
    def test_db(self):
        self.dir_test_db.clear()
        if self.db_server.text().strip() != '':
            self.dir_test_db['server'] = self.db_server.text().strip()
        if self.db_port.text().strip() != '':
            try:
                port = int(self.db_port.text().strip())
            except Exception as error:
                QMessageBox.warning(self,'端口错误','服务器端口是数字,您输入错误.')
                return
            self.dir_test_db['port'] = port
        if self.db_user.text().strip() != '':
            self.dir_test_db['user'] = self.db_user.text().strip()
        if self.db_password.text().strip() != '':
            self.dir_test_db['password'] = self.db_password.text().strip()
        try:
            conn = pymssql.connect(**self.dir_test_db)
        except Exception as error:
            QMessageBox.warning(self,'错误','无法链接数据库.')
            return
        self.test_label.setText('')
        QMessageBox.about(self,'链接成功','成功链接数据库.')
        self.db_test_flag = 1
        conn.close()
    #数据库保存按钮
    def save_db(self):
        if self.db_test_flag < 0:
            QMessageBox.warning(self,'提示','保存数据库信息之前,需要先测试数据库链接.\n数据库链接正常之后,才能保存信息.')
            return
        self.dir_db = self.dir_test_db.copy()
        with open('.//DATA//DBinfo.pik','wb') as db_file:
            pickle.dump(self.dir_db, db_file)
        self.db_test_flag = -1
        QMessageBox.about(self,'提示','保存信息成功.')

本文标题:pyqt5_站点管理_db_windows
文章链接:http://cdiso.cn/article/psoose.html

其他资讯