def modify_clicked(self): # 连接数据库 把修改写进数据库 # 首先获取文本框数据 rem = Re_manager() name = self.user_lineEdit.text() # 没有变,不需要加判断语句 dept = self.dept_lineEdit.text() ip = self.ip_lineEdit.text() mac = self.mac_lineEdit.text() if dept == '' or ip == '' or mac == '': print('修改内容需要填写') reply = QMessageBox.about(self, '提示', '请完善修改内容') else: dept = rem.is_dept(dept) ip = rem.is_ip(ip) mac = rem.is_mac(mac) if dept == None or ip == None or mac == None: print('格式不符合要求') reply = QMessageBox.about(self, '提示', '请按照格式修改相关内容') else: # 将数据更新到数据库 mm = Mysql_manager() with mm: sql = 'update em_info set em_dept = %s,em_ip = %s,em_mac = %s where em_name = %s' mm.db_exe(sql, (dept, ip, mac, name)) print('信息更新') reply = QMessageBox.about(self, '提示', f'{name}的信息修改成功') self.close() # 修改完成关闭窗口,不然文本框还有内容不好处理
def register_clicked(self): # 两个类的同名函数,测试同名函数的影响 # 获取文本框值,先做正则判断,再写入数据库 account = self.account_lineEdit.text() passwd = self.passwd_lineEdit.text() confirm = self.confirm_lineEdit.text() # 先判断输入不能为空 测试不配对else if account == '' or passwd == '' or confirm == '': print('输入不能为空') reply = QMessageBox.about(self, '提示', '输入不能为空') # 判断两次密码是否一致 elif passwd != confirm: print('密码输入不一致') reply = QMessageBox.about(self, '提示', '密码输入不一致') # 判断是否符合正则,这里必须在else下面了。防止不必要的执行 else: # 然后进行正则判断 rem = Re_manager() account = rem.is_account(account) passwd = rem.is_passwd(passwd) if account == None or passwd == None: print('帐号或者密码不符合要求') reply = QMessageBox.about(self, '提示', '帐号或者密码不符合要求') else: mm = Mysql_manager() with mm: sql = 'insert into user_info values(%s,%s,%s)' mm.db_exe(sql, (account, passwd, 1)) print('帐号注册成功') reply = QMessageBox.about(self, '提示', '帐号注册成功')
def query_clicked(self): if self.user_lineEdit == '' and self.ip_lineEdit == '': print('请输入查询条件') else: # 根据单选按钮判断 mm = Mysql_manager() with mm: if self.name_radioButton.isChecked( ) == True: # 注意这里的判断条件,后面括号不能少 sql = 'select * from em_info where em_name = %s' name = self.user_lineEdit.text() result = mm.db_exe(sql, name) if mm.cur.rowcount == 0: print('没有相关信息') reply = QMessageBox.about(self, '提示', '查不到相关信息') else: # 这个时候把result发射到主窗口 print(type(result)) print(result) self.mysignal.emit(result) else: # 通过IP查询 sql = 'select * from em_info where em_ip = %s' ip = self.ip_lineEdit.text() print(f'this {ip}') result = mm.db_exe(sql, ip) if mm.cur.rowcount == 0: print('没有相关信息') reply = QMessageBox.about(self, '提示', '查不到相关IP信息') else: self.mysignal.emit(result)
def add_clicked(self): # 获取文本框的值 name = self.user_lineEdit.text() dept = self.dept_lineEdit.text() ip = self.ip_lineEdit.text() mac = self.mac_lineEdit.text() # 进行输入值检查 if name == '' or dept == '' or ip == '' or mac == '': print('关键项输入不能为空') reply = QMessageBox.about(self, '提示', '关键项输入不能为空') else: rem = Re_manager() name = rem.is_name(name) dept = rem.is_dept(dept) ip = rem.is_ip(ip) mac = rem.is_mac(mac) if name == None or dept == None or ip == None or mac == None: print('格式不符合要求') reply = QMessageBox.about(self, '提示', '格式不符合正则要求') else: # 都符合要求,准备写入数据库 mm = Mysql_manager() with mm: sql = 'insert into em_info values(%s,%s,%s,%s)' mm.db_exe(sql, (name, dept, ip, mac)) print('添加信息成功') reply = QMessageBox.about(self, '提示', '员工信息添加成功') # 添加成功后清空文本框 self.user_lineEdit.setText('') self.dept_lineEdit.setText('') self.ip_lineEdit.setText('') self.mac_lineEdit.setText('')
def login_clicked(self): # 首先从文本框获取值,然后交给正则类进行判断 account = self.account_lineEdit.text() passwd = self.passwd_lineEdit.text() if account == '' or passwd == '': # 第一层判断是否为空 print('输入不能为空') reply = QMessageBox.about(self, '注意', '帐号或密码不能为空') else: rem = Re_manager() account = rem.is_account(account) # 把自己传递进去做判断 passwd = rem.is_passwd(passwd) # 测试 print('**') print(passwd) if account == None or passwd == None: # 第二层判断格式问题 print('帐号或者密码格式错误') reply = QMessageBox.about(self, '注意', '帐号或者密码格式错误') else: # 第三层判断帐号或者密码是否正确 mm = Mysql_manager() with mm: sql = 'select * from user_info where user_name = %s and user_passwd = %s' result = mm.db_exe(sql, (account, passwd)) if mm.cur.rowcount == 0: print('帐号密码错误') reply = QMessageBox.about(self, '注意', '帐号密码错误') else: print(result) print('验证成功,可以登录') # 这里才是跳转主窗口的语句模块 self.em = Em_manager() self.em.show() login.close()
def import_excel(self,path): # 表格对象 self.wb = load_workbook(path) # 表单(子表)对象 self.ws = self.wb.active # 建立数据库连接 mm = Mysql_manager() sql = 'insert into em_info values(%s,%s,%s,%s)' with mm: # 循环读出,循环写入 row_num = self.ws.max_row # 获取子表行数 print('测试') print(row_num) # 注意表格计数是从1开始,不是0 for i in range(2,row_num+1): name = self.ws.cell(row = i,column = 1).value dept = self.ws.cell(row = i,column = 2).value ip = self.ws.cell(row = i,column = 3).value mac = self.ws.cell(row = i,column = 4).value mm.db_exe(sql,(name,dept,ip,mac)) print('数据成功导入到数据库')
def del_clicked(self): name = self.user_lineEdit.text() ip = self.ip_lineEdit.text() if name == '' and ip == '': print('请至少输入一个条件') reply = QMessageBox.about(self, '提示', '至少输入一个条件来删除') else: # 正则判断 mm = Mysql_manager() rem = Re_manager() name = rem.is_name(name) ip = rem.is_ip(ip) if name == None and ip == None: print('输入格式不正确') reply = QMessageBox.about(self, '提示', '输入格式不正确') elif name == None: # 这里开始至少一个不为0 with mm: sql = 'delete from em_info where em_ip = %s' mm.db_exe(sql, ip) if mm.cur.rowcount == 0: print('没有该IP相关信息') reply = QMessageBox.about(self, '提示', '没有该IP的相关信息') else: print('删除成功') reply = QMessageBox.about(self, '提示', f'{ip}的相关信息已删除') elif ip == None: with mm: sql = 'delete from em_info where em_name = %s' mm.db_exe(sql, name) if mm.cur.rowcount == 0: print('没有该员工信息') reply = QMessageBox.about(self, '提示', '数据库没有该员工信息') else: print('删除成功') reply = QMessageBox.about(self, '提示', f'{name}的相关信息已删除') else: # 最后一种情况,两个文本框都有内容 with mm: sql = 'select * from em_info where em_name = %s and em_ip = %s' mm.db_exe(sql, (name, ip)) if mm.cur.rowcount == 0: print('没有相关信息') reply = QMessageBox.about(self, '提示', '没有相关信息') else: print('删除成功') reply = QMessageBox.about(self, '提示', '信息删除成功') # 无论如何,清空文本框 self.user_lineEdit.setText('') self.ip_lineEdit.setText('')