def alter_managerself # 获取选中的行号 selected_row = selftable_managercurrentRow if selected_row 0 QMessageBoxwarningself 警告 请先选择要修改的读者! return # 获取选中行的借书证
def alter_manager(self): # 获取选中的行号 selected_row = self.table_manager.currentRow() if selected_row < 0: QMessageBox.warning(self, '警告', '请先选择要修改的读者!') return # 获取选中行的借书证号 manager_id = self.table_manager.item(selected_row, 0).text() # 获取用户输入的信息 new_password = self.table_manager.item(selected_row, 1).text() beizhu = self.table_manager.item(selected_row, 2).text() old_password, okPressed = QInputDialog.getText(self, "请输入旧密码", "旧密码:", QLineEdit.Password) if not okPressed: QMessageBox.warning(self, "提示", "请输入旧密码!") return
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', password='123456', db='library', charset='utf8')
cursor = conn.cursor()
try:
# 查询用户输入的用户名和旧密码是否匹配
sql = "SELECT * FROM lad WHERE 帐号='{}' AND 密码='{}'".format(manager_id, old_password)
cursor.execute(sql)
result = cursor.fetchone()
if result:
# 如果用户名和旧密码匹配,则更新密码和备注
sql = "UPDATE lad SET 密码='{}', 备注='{}' WHERE 帐号='{}'".format(new_password, beizhu, manager_id)
cursor.execute(sql)
conn.commit()
QMessageBox.information(self, "提示", "更新成功!")
else:
# 如果用户名和旧密码不匹配,则提示用户
QMessageBox.warning(self, "提示", "旧密码输入不正确!")
except Exception as e:
# 如果出现异常,则回滚操作并提示用户
conn.rollback()
QMessageBox.warning(self, "提示", "更新失败:" + str(e))
finally:
# 关闭数据库连接
conn.close(
原文地址: https://www.cveoy.top/t/topic/hnMC 著作权归作者所有。请勿转载和采集!