溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

pyqt5_站點(diǎn)管理_db_windows

發(fā)布時(shí)間:2020-07-18 05:43:10 來源:網(wǎng)絡(luò) 閱讀:403 作者:we17ha 欄目:數(shù)據(jù)庫(kù)
from PyQt5.QtWidgets import QWidget,QLabel,QLineEdit,QGroupBox,QGridLayout,QVBoxLayout,QPushButton,QMessageBox
import pickle,pymssql,os,threading,time
#定義數(shù)據(jù)庫(kù)窗口
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 = {}
        #初始化數(shù)據(jù)庫(kù)鏈接信息
        self.db_test_flag = -1
        self.dir_test_db = {}
        self.initUI()
        self.initDB()
    def initUI(self):
        #初始化頁(yè)面
        db_server_label = QLabel('數(shù)據(jù)庫(kù)地址:')
        db_port_label = QLabel('端口:')
        db_user_label = QLabel('數(shù)據(jù)庫(kù)賬號(hào):')
        db_password_label = QLabel('數(shù)據(jù)庫(kù)密碼:')
        self.db_password.setEchoMode(QLineEdit.Password)
        db_test_button = QPushButton('測(cè)試數(shù)據(jù)庫(kù)鏈接')
        db_input_button = QPushButton('保存數(shù)據(jù)庫(kù)鏈接')
        db_test_button.clicked.connect(self.test_db)
        db_input_button.clicked.connect(self.save_db)
        db_dbinfo_group = QGroupBox('MSSQL數(shù)據(jù)庫(kù)')
        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)
    #初始化數(shù)據(jù)庫(kù)鏈接信息
    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
    #數(shù)據(jù)庫(kù)測(cè)試按鈕
    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,'端口錯(cuò)誤','服務(wù)器端口是數(shù)字,您輸入錯(cuò)誤.')
                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,'錯(cuò)誤','無法鏈接數(shù)據(jù)庫(kù).')
            return
        self.test_label.setText('')
        QMessageBox.about(self,'鏈接成功','成功鏈接數(shù)據(jù)庫(kù).')
        self.db_test_flag = 1
        conn.close()
    #數(shù)據(jù)庫(kù)保存按鈕
    def save_db(self):
        if self.db_test_flag < 0:
            QMessageBox.warning(self,'提示','保存數(shù)據(jù)庫(kù)信息之前,需要先測(cè)試數(shù)據(jù)庫(kù)鏈接.\n數(shù)據(jù)庫(kù)鏈接正常之后,才能保存信息.')
            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,'提示','保存信息成功.')


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI