''' import sys import pymysql from PyQt5.QtCore import Qt from PyQt5.QtGui import QStandardItemModel, QStandardItem from PyQt5.QtWidgets import QApplication, QMainWindow, QTableView class MainWindow(QMainWindow): def __init__(self): super().__init__() self.setWindowTitle("Personal Information") # 创建 QTableView delete self.table_view = QTableView(self) self.setCentralWidget(self.table_view) # 设置模型 self.model = QStandardItemModel(0, 7) self.model.setHorizontalHeaderLabels(['身份证号','用户名', '性别', '用户年龄', '电话号码','户籍','密码']) self.table_view.setModel(self.model) # 加载数据 self.load_data() # 使表格可编辑 self.table_view.setEditTriggers(QTableView.AllEditTriggers) # 捕捉模型数据改变信号 self.model.itemChanged.connect(self.update_database) #connect mysql and enquire database data def load_data(self): try: connection = pymysql.connect( host='127.0.0.1', user='root', password='20021220', database='pybank' ) cursor = connection.cursor() cursor.execute("SELECT * FROM customer") data = cursor.fetchall() cursor.close() connection.close() for row in data: items = [QStandardItem(str(field)) for field in row] for item in items: item.setTextAlignment(Qt.AlignCenter) self.model.appendRow(items) except Exception as e: print(f"Error loading data: {e}") def update_database(self, item): row = item.row() col = item.column() new_value = item.text() print(row,col,new_value) try: # 获取主键(第一列是主键) primary_key = self.model.item(row, 0).text() #map column names columns=['cid', 'cname', 'csex', 'cage', 'cnumber', 'caddress', 'cpsw'] column_name=columns[col] # 更新数据库 connection = pymysql.connect( host='127.0.0.1', user='root', password='20021220', database='pybank' ) cursor = connection.cursor() query = f"UPDATE account SET {column_name} = %s WHERE cid = %s" cursor.execute(query, (new_value, primary_key)) connection.commit() cursor.close() connection.close() print(f"Updated row {row}, column {col} with new value {new_value}") except Exception as e: print(f"Error updating database: {e}") if __name__ == '__main__': app = QApplication(sys.argv) main_window = MainWindow() main_window.resize(800, 600) main_window.show() sys.exit(app.exec_()) ''' def add_acc(self): global user_now try: accid = self.generator_accid() accbalance = self.ui.get_savings.text() current_date = datetime.now().date() acc_start = current_date.strftime('%Y-%m-%d') cid = user_now acctime = self.ui.deposit_time.currentText() # 获取 QGroupBox 中选中的 QRadioButton typebox = self.ui.type_box acctype = None for button in typebox.findChildren(QRadioButton): if button.isChecked(): acctype = button break if acctype is None: raise ValueError("未选择账户类型") return acc_type = acctype.text() # 确认选择活期类型时长只能选择不定期 if acc_type == "活期" and acctime != "不定期": raise ValueError("活期账户的时长只能选择不定期") return # 输出到控制台 print(accid, accbalance, acc_start, acctime, cid, acc_type) # 将新账户信息插入数据库 try: db = pymysql.connect(host='127.0.0.1', user='root', password='20021220', database='pybank') cur = db.cursor() query = "INSERT INTO account (accid, accbalance, acctime_start, accdur_time, cid, acc_type) VALUES (%s, %s, %s, %s, %s, %s)" cur.execute(query, (accid, accbalance, acc_start, acctime, cid, acc_type)) db.commit() db.close() QMessageBox.information(self, "成功", "账户添加成功") except Exception as db_error: print(f"数据库错误: {db_error}") QMessageBox.critical(self, "错误", "添加账户到数据库失败") except Exception as e: print(e) QMessageBox.critical(self, "错误", f"发生错误: {e}") # 将信息写入文件 try: filename = f"{user_now}.txt" s = f"用户于{acc_start},开通账号:{accid},存取金额为{accbalance},期限为{acctime}\n" with open(filename, "a", encoding='UTF-8') as file: file.write(s) file.write("-" * 60 + "\n") print(f"信息已写入文件: {filename}") except Exception as file_error: print(f"文件写入错误: {file_error}") self.ui.get_savings.clear()