How to use MySQL and Python to develop a simple online examination system
Introduction:
With the development of technology and the popularity of the Internet, more and more Schools and training institutions are beginning to use online exams to assess student learning. This article will introduce how to use MySQL and Python to develop a simple online examination system, and provide specific code examples.
1. Requirements analysis:
Before developing an online examination system, it is first necessary to clarify the system requirements. A simple online examination system needs to implement the following functions:
2. System design:
System architecture:
The online examination system can use Python as the back-end development language and use the Flask framework to quickly build web applications. The system architecture is as follows:
--------> 考试页面 |
User----> Login/Registration----> Homepage--------> Exam Records/Results
| --------> 管理页面
3. System implementation:
4. Code example:
The following is a simple code example that implements user registration and login functions:
'''
from flask import Flask , request, render_template, redirect, url_for
from flask_mysqldb import MySQL
app = Flask(__name__)
app.config['MYSQL_HOST'] = 'localhost'
app.config[' MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = 'password'
app.config['MYSQL_DB'] = 'online_exam'
mysql = MySQL(app)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/register', methods=['GET', 'POST'] )
def register():
if request.method == 'POST': username = request.form['username'] password = request.form['password'] cur = mysql.connection.cursor() cur.execute("INSERT INTO User(username, password) VALUES(%s, %s)", (username, password)) mysql.connection.commit() cur.close() return redirect(url_for('index')) 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'] cur = mysql.connection.cursor() cur.execute("SELECT * FROM User WHERE username = %s AND password = %s", (username, password)) user = cur.fetchone() if user: return 'Login success' else: return 'Login failed' cur.close() return render_template('login.html')
ifname== '__main__':
app.run()
'''
Summary:
By combining MySQL and Python, you can easily develop a Simple online exam system. This article introduces the requirements analysis, system design and implementation steps of the online examination system, and provides a code example for user registration and login. Readers can modify and improve it according to actual needs and specific situations.
The above is the detailed content of How to develop a simple online exam system using MySQL and Python. For more information, please follow other related articles on the PHP Chinese website!