Home >Database >Mysql Tutorial >How to develop a simple online questionnaire using MySQL and Python
How to use MySQL and Python to develop a simple online questionnaire
Introduction
Online questionnaires are widely used in modern society to collect user information Views, feedback and comments. This article will introduce how to use MySQL and Python to develop a simple online questionnaire system, and provide relevant code examples.
1. Database design
Create a database named survey:
CREATE DATABASE survey;
Create a database named questions and responses Table:
Create questions table, used to store questionnaire questions:
CREATE TABLE questions ( id INT PRIMARY KEY AUTO_INCREMENT, question_text VARCHAR(255) NOT NULL );
Create responses table, used to store user answers:
CREATE TABLE responses ( id INT PRIMARY KEY AUTO_INCREMENT, question_id INT, response_text VARCHAR(255) NOT NULL, FOREIGN KEY (question_id) REFERENCES questions(id) );
2. Python code implementation
Import the necessary libraries:
import mysql.connector from mysql.connector import Error from flask import Flask, request, render_template
Connect to the MySQL database:
def create_connection(): connection = None try: connection = mysql.connector.connect( host='localhost', database='survey', user='your_username', password='your_password' ) if connection.is_connected(): print('Connected to MySQL database') except Error as e: print(e) return connection
Create Flask application and routing:
app = Flask(__name__) @app.route('/') def home(): # 获取所有的问题 connection = create_connection() cursor = connection.cursor() query = 'SELECT * FROM questions' cursor.execute(query) questions = cursor.fetchall() cursor.close() connection.close() return render_template('index.html', questions=questions) @app.route('/survey', methods=['POST']) def survey(): # 获取用户的答案 connection = create_connection() cursor = connection.cursor() response_text = request.form['response'] question_id = request.form['question_id'] query = 'INSERT INTO responses (question_id, response_text) VALUES (%s, %s)' cursor.execute(query, (question_id, response_text)) connection.commit() cursor.close() connection.close() return 'Thank you for your response!' if __name__ == '__main__': app.run()
Create front-end page (index.html):
<!DOCTYPE html> <html> <head> <title>Survey</title> </head> <body> <h1>Survey</h1> <form action="/survey" method="POST"> {% for question in questions %} <p>{{ question[1] }}</p> <input type="hidden" name="question_id" value="{{ question[0] }}"> <input type="text" name="response" required> {% endfor %} <input type="submit" value="Submit"> </form> </body> </html>
3. Run and test
Run the Python code in the terminal:
python survey_app.py
Conclusion
This article introduces how to use MySQL and Python to develop a simple online questionnaire system. Through database design and related code implementation, users' opinions, feedback and opinions can be easily collected. By enriching the design and functionality of the front-end page, the user experience of the questionnaire can be further improved. I hope this article can be helpful to readers and inspire more innovative ideas.
The above is the detailed content of How to develop a simple online questionnaire using MySQL and Python. For more information, please follow other related articles on the PHP Chinese website!