编写一个能够增删改查的学生管理系统
时间: 2023-10-16 10:10:48 浏览: 55
好的,以下是一个简单的学生管理系统,支持增加学生、修改学生、删除学生和查询学生信息的功能。
```
import mysql.connector
# 连接数据库
mydb = mysql.connector.connect(
host="localhost",
user="yourusername",
password="yourpassword",
database="mydatabase"
)
# 创建学生表
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE students (id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, gender ENUM('Male', 'Female') NOT NULL, birthday DATE NOT NULL, email VARCHAR(50), phone VARCHAR(20), address VARCHAR(100), department VARCHAR(50), major VARCHAR(50), grade INT, class_no INT)")
# 添加学生
def add_student(name, gender, birthday, email, phone, address, department, major, grade, class_no):
sql = "INSERT INTO students (name, gender, birthday, email, phone, address, department, major, grade, class_no) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
val = (name, gender, birthday, email, phone, address, department, major, grade, class_no)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "记录插入成功。")
# 修改学生信息
def update_student(id, email, phone, address):
sql = "UPDATE students SET email = %s, phone = %s, address = %s WHERE id = %s"
val = (email, phone, address, id)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "记录更新成功。")
# 删除学生
def delete_student(id):
sql = "DELETE FROM students WHERE id = %s"
val = (id,)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "记录删除成功。")
# 查询学生信息
def query_student(name):
sql = "SELECT * FROM students WHERE name = %s"
val = (name,)
mycursor.execute(sql, val)
myresult = mycursor.fetchall()
for x in myresult:
print(x)
# 测试
add_student("张三", "Male", "2002-01-01", "zhangsan@example.com", "12345678901", "北京市朝阳区", "计算机科学与技术系", "计算机科学与技术", 2019, 1)
update_student(1, "zhangsan_new@example.com", "12345678901", "北京市海淀区")
delete_student(1)
query_student("张三")
```
以上代码使用了Python和MySQL数据库,通过连接数据库并创建学生表来实现学生信息的管理。其中,add_student()函数实现了添加学生的功能,update_student()函数实现了修改学生信息的功能,delete_student()函数实现了删除学生的功能,query_student()函数实现了查询学生信息的功能。最后,通过测试函数来验证这些功能是否正常。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)