博客二:宿舍管理系统的操作
# 宿舍管理系统的操作
宿舍管理系统提供了一系列的操作功能,包括添加宿舍、添加学生、删除学生、修改宿舍和刷新数据等。这些操作通过与后端数据库的交互实现数据的增删改查。
## 添加宿舍
```python
def add_dorm(self, building, area, max_number):
try:
with self.connection.cursor() as cursor:
cursor.execute("""
INSERT INTO tb_dorm (building, area, max_number)
VALUES (%s, %s, %s)
""", (building, area, max_number))
self.connection.commit()
self._load_data()
return cursor.lastrowid
except Exception as e:
self.connection.rollback()
raise e
def add_student(self, dorm_id, name, gender, class_): # 修复参数名
try:
with self.connection.cursor() as cursor:
cursor.execute("""
INSERT INTO tb_student (dorm_id, name, gender, class)
VALUES (%s, %s, %s, %s)
""", (dorm_id, name, gender, class_))
if self._update_dorm_people(dorm_id, 1):
self.connection.commit()
self._load_data()
return cursor.lastrowid
except Exception as e:
self.connection.rollback()
raise e
def delete_student(self, student_id):
try:
with self.connection.cursor() as cursor:
cursor.execute("SELECT dorm_id FROM tb_student WHERE id = %s", (student_id,))
if dorm_id := cursor.fetchone():
cursor.execute("DELETE FROM tb_student WHERE id = %s", (student_id,))
if cursor.rowcount > 0:
self._update_dorm_people(dorm_id[0], -1)
self.connection.commit()
self._load_data()
return True
return False
except Exception as e:
self.connection.rollba