酷代码 AI
菜单

帮我写一个关于学校查分的代码(要求设计一个B/S(浏览器/服务器)交互式动态网站。要求网站不要脱离数据库而独立存在,要体现出交互性,比如说要有用户注册、登录、不同的用户授予不同的权限、聊天室、留言板、论坛、搜索引擎等其中的几项内容,并能够在网络上发布。)用SQL SERVER写要源代码文件

以下是一个基于Python的Flask框架和SQL Server数据库构建学校查分系统的示例代码,此系统包含用户注册、登录、查分功能,同时为不同用户授予不同权限。 1. 数据库设计 首先在SQL Server中创建所需的表。可以使用SQL Server Management Studio或以下SQL语句: ```sql -- 创建用户表 CREATE TABLE Users ( UserID INT IDENTITY(1,1) PRIMARY KEY, Username NVARCHAR(50) NOT NULL UNIQUE, Password NVARCHAR(50) NOT NULL, UserType NVARCHAR(20) NOT NULL -- 例如:学生、教师、管理员 ); -- 创建成绩表 CREATE TABLE Scores ( ScoreID INT IDENTITY(1,1) PRIMARY KEY, UserID INT, CourseName NVARCHAR(50) NOT NULL, Score FLOAT NOT NULL, FOREIGN KEY (UserID) REFERENCES Users(UserID) ); ``` 2. Python Flask代码 ```python from flask import Flask, render_template, request, redirect, session import pyodbc app = Flask(__name__) app.secret_key = 'your_secret_key' # 连接到SQL Server数据库 conn = pyodbc.connect('DRIVER={SQL Server};SERVER=your_server_name;DATABASE=your_database_name;UID=your_username;PWD=your_password') cursor = conn.cursor() # 注册页面 @app.route('/register', methods=['GET', 'POST']) def register(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] user_type = request.form['user_type'] # 插入新用户到数据库 cursor.execute("INSERT INTO Users (Username, Password, UserType) VALUES (?,?,?)", (username, password, user_type)) conn.commit() return redirect('/login') return render_template('register.html') # 登录页面 @app.route('/login', methods=['GET', 'POST']) def login(): if request.method == 'POST': username = request.form['username'] password = request.form['password'] cursor.execute("SELECT UserID, UserType FROM Users WHERE Username =? AND Password =?", (username, password)) user = cursor.fetchone() if user: session['user_id'] = user[0] session['user_type'] = user[1] return redirect('/dashboard') else: return "Invalid username or password" return render_template('login.html') # 仪表盘页面 @app.route('/dashboard') def dashboard(): if 'user_id' not in session: return redirect('/login') user_type = session['user_type'] if user_type == '学生': cursor.execute("SELECT CourseName, Score FROM Scores WHERE UserID =?", session['user_id']) scores = cursor.fetchall() return render_template('student_dashboard.html', scores=scores) elif user_type == '教师': return "教师仪表盘" elif user_type == '管理员': return "管理员仪表盘" # 注销 @app.route('/logout') def logout(): session.pop('user_id', None) session.pop('user_type', None) return redirect('/login') if __name__ == '__main__': app.run(debug=True) ``` 3. HTML模板文件 `register.html` ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>注册</title> </head> <body> <h1>注册</h1> <form method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br> <label for="user_type">用户类型:</label> <select id="user_type" name="user_type"> <option value="学生">学生</option> <option value="教师">教师</option> <option value="管理员">管理员</option> </select><br> <input type="submit" value="注册"> </form> </body> </html> ``` `login.html` ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>登录</title> </head> <body> <h1>登录</h1> <form method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required><br> <label for="password">密码:</label> <input type="password" id="password" name="password" required><br> <input type="submit" value="登录"> </form> </body> </html> ``` `student_dashboard.html` ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>学生仪表盘</title> </head> <body> <h1>成绩查询</h1> <table> <thead> <tr> <th>课程名称</th> <th>成绩</th> </tr> </thead> <tbody> {% for score in scores %} <tr> <td>{{ score[0] }}</td> <td>{{ score[1] }}</td> </tr> {% endfor %} </tbody> </table> <a href="/logout">注销</a> </body> </html> ``` 额外需要注意的逻辑点 1. 密码安全:在实际应用中,不要明文存储密码,应该使用加密算法(如哈希)对密码进行加密存储。 2. 错误处理:在数据库操作中,要添加适当的错误处理机制,以防止因数据库连接问题或SQL语句执行失败导致程序崩溃。 3. 会话管理:确保会话的安全性,避免会话劫持等安全问题,可以使用HTTPS协议来保护会话信息。 [2025-06-02 08:00:48 | AI写代码神器 | 1465点数解答]

服务商
相关提问
发财导航,免费问AI
实用工具查看更多