学生信息管理系统建表和代码
时间: 2023-09-24 15:11:39 浏览: 90
建表语句:
```sql
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender ENUM('男', '女') NOT NULL,
age INT NOT NULL,
major VARCHAR(50) NOT NULL,
grade INT NOT NULL
);
```
代码示例:
```python
import pymysql
# 连接数据库
conn = pymysql.connect(host='localhost', user='root', password='password', database='testdb', charset='utf8mb4')
cursor = conn.cursor()
# 添加学生信息
def add_student(name, gender, age, major, grade):
sql = "INSERT INTO student (name, gender, age, major, grade) VALUES (%s, %s, %s, %s, %s)"
cursor.execute(sql, (name, gender, age, major, grade))
conn.commit()
# 查询学生信息
def query_student(name):
sql = "SELECT * FROM student WHERE name = %s"
cursor.execute(sql, name)
result = cursor.fetchone()
return result
# 修改学生信息
def update_student(name, major):
sql = "UPDATE student SET major = %s WHERE name = %s"
cursor.execute(sql, (major, name))
conn.commit()
# 删除学生信息
def delete_student(name):
sql = "DELETE FROM student WHERE name = %s"
cursor.execute(sql, name)
conn.commit()
# 关闭数据库连接
cursor.close()
conn.close()
```
注意:以上代码仅为示例,实际应用中需要考虑异常处理、数据验证等问题。
阅读全文