学生信息管理系统课程设计代码实现
时间: 2023-09-20 08:06:23 浏览: 106
很抱歉,我无法直接提供完整的代码实现,因为这是一个需要根据具体需求进行设计的课程项目。以下是一些可能的实现思路和参考代码片段:
1. 数据库设计
首先需要设计数据库,包括学生信息、课程信息、成绩信息等数据库表。可以使用 MySQL、SQLite 等关系型数据库,或使用 MongoDB 等非关系型数据库。
以下是一个 MySQL 数据库设计的示例:
```sql
-- 学生信息表
CREATE TABLE student (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
gender ENUM('男', '女') NOT NULL,
birthday DATE NOT NULL,
phone VARCHAR(20) NOT NULL,
address VARCHAR(100) NOT NULL
);
-- 课程信息表
CREATE TABLE course (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50) NOT NULL,
teacher VARCHAR(50) NOT NULL,
credit INT NOT NULL,
description VARCHAR(100)
);
-- 成绩信息表
CREATE TABLE score (
id INT PRIMARY KEY AUTO_INCREMENT,
student_id INT NOT NULL,
course_id INT NOT NULL,
score INT NOT NULL,
FOREIGN KEY (student_id) REFERENCES student(id),
FOREIGN KEY (course_id) REFERENCES course(id)
);
```
2. 后端实现
后端主要负责与数据库交互,并提供 API 接口供前端调用。可以使用 Python、Java、PHP 等语言实现后端,选择合适的框架进行开发。
以下是一个使用 Python Flask 框架实现的示例:
```python
from flask import Flask, request, jsonify
import pymysql
app = Flask(__name__)
# 数据库连接配置
DB_HOST = 'localhost'
DB_USER = 'root'
DB_PASSWORD = 'password'
DB_NAME = 'student'
# 连接数据库
db = pymysql.connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
# 获取所有学生信息
@app.route('/api/students', methods=['GET'])
def get_students():
cur = db.cursor()
cur.execute('SELECT * FROM student')
students = cur.fetchall()
cur.close()
return jsonify(students)
# 添加学生信息
@app.route('/api/students', methods=['POST'])
def add_student():
data = request.get_json()
name = data['name']
gender = data['gender']
birthday = data['birthday']
phone = data['phone']
address = data['address']
cur = db.cursor()
cur.execute('INSERT INTO student (name, gender, birthday, phone, address) VALUES (%s, %s, %s, %s, %s)', (name, gender, birthday, phone, address))
db.commit()
cur.close()
return jsonify({'message': 'success'})
# 修改学生信息
@app.route('/api/students/<int:id>', methods=['PUT'])
def update_student(id):
data = request.get_json()
name = data['name']
gender = data['gender']
birthday = data['birthday']
phone = data['phone']
address = data['address']
cur = db.cursor()
cur.execute('UPDATE student SET name=%s, gender=%s, birthday=%s, phone=%s, address=%s WHERE id=%s', (name, gender, birthday, phone, address, id))
db.commit()
cur.close()
return jsonify({'message': 'success'})
# 删除学生信息
@app.route('/api/students/<int:id>', methods=['DELETE'])
def delete_student(id):
cur = db.cursor()
cur.execute('DELETE FROM student WHERE id=%s', id)
db.commit()
cur.close()
return jsonify({'message': 'success'})
```
3. 前端实现
前端主要负责展示数据,并与后端交互。可以使用 HTML、CSS、JavaScript 等技术实现前端页面,选择合适的框架进行开发。
以下是一个使用 Vue.js 框架实现的示例:
```html
<template>
<div>
<h1>学生信息管理系统</h1>
<table>
<thead>
<tr>
<th>ID</th>
<th>姓名</th>
<th>性别</th>
<th>生日</th>
<th>电话</th>
<th>地址</th>
</tr>
</thead>
<tbody>
<tr v-for="student in students" :key="student.id">
<td>{{ student.id }}</td>
<td>{{ student.name }}</td>
<td>{{ student.gender }}</td>
<td>{{ student.birthday }}</td>
<td>{{ student.phone }}</td>
<td>{{ student.address }}</td>
<td><button @click="editStudent(student)">编辑</button></td>
<td><button @click="deleteStudent(student)">删除</button></td>
</tr>
</tbody>
</table>
<form v-if="showForm" @submit.prevent="submitForm">
<label>姓名:</label>
<input type="text" v-model="formData.name" required>
<label>性别:</label>
<select v-model="formData.gender" required>
<option value="男">男</option>
<option value="女">女</option>
</select>
<label>生日:</label>
<input type="date" v-model="formData.birthday" required>
<label>电话:</label>
<input type="tel" v-model="formData.phone" required>
<label>地址:</label>
<input type="text" v-model="formData.address" required>
<button type="submit">提交</button>
</form>
<button @click="addStudent">添加学生</button>
</div>
</template>
<script>
export default {
data() {
return {
students: [],
showForm: false,
formData: {
name: '',
gender: '男',
birthday: '',
phone: '',
address: ''
},
editId: null
}
},
mounted() {
this.getStudents()
},
methods: {
getStudents() {
fetch('/api/students')
.then(response => response.json())
.then(data => {
this.students = data
})
},
addStudent() {
this.showForm = true
this.formData = {
name: '',
gender: '男',
birthday: '',
phone: '',
address: ''
}
},
editStudent(student) {
this.showForm = true
this.formData = {
name: student.name,
gender: student.gender,
birthday: student.birthday,
phone: student.phone,
address: student.address
}
this.editId = student.id
},
deleteStudent(student) {
if (confirm(`确定要删除学生 ${student.name} 吗?`)) {
fetch(`/api/students/${student.id}`, { method: 'DELETE' })
.then(response => response.json())
.then(data => {
this.getStudents()
})
}
},
submitForm() {
if (this.editId) {
fetch(`/api/students/${this.editId}`, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.formData)
})
.then(response => response.json())
.then(data => {
this.getStudents()
this.showForm = false
this.editId = null
})
} else {
fetch('/api/students', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(this.formData)
})
.then(response => response.json())
.then(data => {
this.getStudents()
this.showForm = false
})
}
}
}
}
</script>
```
阅读全文