您好,登錄后才能下訂單哦!
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,'提示','保存信息成功.')
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。