编写可以实现学生成绩简单管理系统程序代码
时间: 2024-03-24 22:39:15 浏览: 58
好的,以下是一个简单的Python程序代码,可以实现学生成绩管理系统:
```python
from flask import Flask, render_template, request
import mysql.connector
app = Flask(__name__)
# 连接MySQL数据库
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="password",
database="students"
)
# 创建数据库表
mycursor = mydb.cursor()
mycursor.execute("CREATE TABLE IF NOT EXISTS students (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), student_id VARCHAR(255), course VARCHAR(255), score INT)")
# 添加一些示例数据
sql = "INSERT INTO students (name, student_id, course, score) VALUES (%s, %s, %s, %s)"
val = [
('张三', '1001', '语文', 90),
('张三', '1001', '数学', 85),
('李四', '1002', '语文', 95),
('李四', '1002', '数学', 92),
('王五', '1003', '语文', 88),
('王五', '1003', '数学', 90)
]
mycursor.executemany(sql, val)
mydb.commit()
# 定义网页路由
@app.route('/')
def home():
return render_template('home.html')
@app.route('/add', methods=['GET', 'POST'])
def add():
if request.method == 'POST':
name = request.form['name']
student_id = request.form['student_id']
course = request.form['course']
score = request.form['score']
sql = "INSERT INTO students (name, student_id, course, score) VALUES (%s, %s, %s, %s)"
val = (name, student_id, course, score)
mycursor.execute(sql, val)
mydb.commit()
return render_template('add.html')
@app.route('/update', methods=['GET', 'POST'])
def update():
if request.method == 'POST':
name = request.form['name']
student_id = request.form['student_id']
course = request.form['course']
score = request.form['score']
sql = "UPDATE students SET score = %s WHERE name = %s AND student_id = %s AND course = %s"
val = (score, name, student_id, course)
mycursor.execute(sql, val)
mydb.commit()
return render_template('update.html')
@app.route('/delete', methods=['GET', 'POST'])
def delete():
if request.method == 'POST':
name = request.form['name']
student_id = request.form['student_id']
course = request.form['course']
sql = "DELETE FROM students WHERE name = %s AND student_id = %s AND course = %s"
val = (name, student_id, course)
mycursor.execute(sql, val)
mydb.commit()
return render_template('delete.html')
@app.route('/query', methods=['GET', 'POST'])
def query():
if request.method == 'POST':
name = request.form['name']
student_id = request.form['student_id']
if name != '':
sql = "SELECT * FROM students WHERE name = %s"
val = (name,)
elif student_id != '':
sql = "SELECT * FROM students WHERE student_id = %s"
val = (student_id,)
else:
return render_template('query.html')
mycursor.execute(sql, val)
results = mycursor.fetchall()
return render_template('query.html', results=results)
return render_template('query.html')
# 运行程序
if __name__ == '__main__':
app.run(debug=True)
```
该程序使用Flask框架实现Web界面,并使用MySQL数据库存储学生成绩信息。您需要在MySQL中创建一个名为`students`的数据库,并在程序中将用户名和密码设置为您的MySQL用户名和密码。程序将自动创建一个名为`students`的表,并向其添加一些示例数据。
程序中包括四个网页路由:`/add`、`/update`、`/delete`和`/query`,分别对应添加、更新、删除和查询学生成绩信息。每个网页路由都对应一个HTML模板文件,用于显示和处理用户界面。其中,查询学生成绩信息需要根据学生姓名或学号来查询,程序会自动判断并执行相应的SQL查询语句。
希望这个程序能够帮助您实现学生成绩管理系统。如有问题,请随时与我联系。
阅读全文