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;
Copy after login 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 );
Copy after loginCreate 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) );
Copy after login
2. Python code implementation
Import the necessary libraries:
import mysql.connector from mysql.connector import Error from flask import Flask, request, render_template
Copy after loginConnect 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
Copy after loginCreate 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()
Copy after loginCreate front-end page (index.html):
<!DOCTYPE html> <html> <head> <title>Survey</title> </head> <body> <h1 id="Survey">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>
Copy after login
3. Run and test
Run the Python code in the terminal:
python survey_app.py
Copy after login- Visit http://localhost:5000 in the browser to see the questionnaire page.
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Golang is better than Python in terms of performance and scalability. 1) Golang's compilation-type characteristics and efficient concurrency model make it perform well in high concurrency scenarios. 2) Python, as an interpreted language, executes slowly, but can optimize performance through tools such as Cython.

Python is easier to learn and use, while C is more powerful but complex. 1. Python syntax is concise and suitable for beginners. Dynamic typing and automatic memory management make it easy to use, but may cause runtime errors. 2.C provides low-level control and advanced features, suitable for high-performance applications, but has a high learning threshold and requires manual memory and type safety management.

Discussion on Hierarchical Structure in Python Projects In the process of learning Python, many beginners will come into contact with some open source projects, especially projects using the Django framework...

Safely handle functions and regular expressions in JSON In front-end development, JavaScript is often required...
