How to Set Up Flask with MySQL in Visual Studio Code (VS Code) | Step-by-Step Guide (2025)



Want to connect **Flask with MySQL** in **Visual Studio Code (VS Code)?** πŸ”₯ In this step-by-step tutorial, we’ll guide you through setting up a Flask web application and integrating it with a MySQL database. This is perfect for **web developers** and **backend engineers** looking to build dynamic, database-driven applications using Flask and MySQL. — ### **πŸ”Ή What You’ll Learn in This Video:** βœ… Installing Flask and MySQL in VS Code βœ… Setting Up a Virtual Environment in VS Code βœ… Installing `Flask-MySQL` Connector βœ… Creating a Flask App with MySQL Integration βœ… Running SQL Queries from Flask βœ… Performing CRUD Operations in Flask — ## **πŸ”Ή Prerequisites** Before starting, ensure you have: βœ”οΈ **Python 3.x** installed → [Download Python](https://www.python.org/downloads/) βœ”οΈ **MySQL Server** installed → [Download MySQL](https://dev.mysql.com/downloads/) βœ”οΈ **VS Code** installed → [Download VS Code](https://code.visualstudio.com/) βœ”οΈ **MySQL Workbench (Optional) for Database Management** — ## **πŸ”Ή Step-by-Step Guide** ### **1️⃣ Install Required Packages** Open **VS Code Terminal** and run the following commands: “`bash
# Create a project directory
mkdir flask-mysql && cd flask-mysql # Create a virtual environment
python -m venv venv
source venv/bin/activate # Windows: venv\Scripts\activate # Install Flask and MySQL Connector
pip install flask flask-mysql mysql-connector-python
“` — ### **2️⃣ Set Up a Flask Application** Create a file named **`app.py`** and add the following Flask code: “`python
from flask import Flask, render_template
import mysql.connector app = Flask(__name__) # Configure MySQL Connection
db = mysql.connector.connect( host="localhost", user="your_username", password="your_password", database="your_database"
)
cursor = db.cursor() @app.route('/')
def home(): cursor.execute("SELECT 'Hello, Flask with MySQL!'") message = cursor.fetchone()[0] return f"[h1]{message}[/h1]" if __name__ == "__main__": app.run(debug=True)
“` Replace `"your_username"`, `"your_password"`, and `"your_database"` with your actual MySQL credentials. — ### **3️⃣ Create a MySQL Database** Log in to MySQL via terminal or MySQL Workbench and run: “`sql
CREATE DATABASE flaskdb;
USE flaskdb; CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100), email VARCHAR(100) UNIQUE
); INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com');
“` — ### **4️⃣ Run Flask App in VS Code** To start the Flask app, run: “`bash
python app.py
“` Visit **http://127.0.0.1:5000/** in your browser, and you should see **"Hello, Flask with MySQL!"** — ### **5️⃣ Fetch Data from MySQL in Flask** Modify **`app.py`** to display database records: “`python
@app.route('/users')
def users(): cursor.execute("SELECT * FROM users") users_list = cursor.fetchall() return f"[h2]Users:[/h2]" + " 8br 9".join([f"{user[1]} – {user[2]}" for user in users_list])
“` Restart Flask and visit **http://127.0.0.1:5000/users** to see database records. — ## **πŸ”Ή Who Is This Tutorial For?** βœ… Python & Flask Developers βœ… Backend Engineers Working with Databases βœ… Anyone Looking to Build Web Apps with Flask & MySQL — ## **πŸ”Ή Resources Mentioned in This Video:** πŸ“Œ Flask Documentation → [https://flask.palletsprojects.com/](https://flask.palletsprojects.com/) πŸ“Œ MySQL Documentation → [https://dev.mysql.com/doc/](https://dev.mysql.com/doc/) πŸ“Œ VS Code Download → [https://code.visualstudio.com/](https://code.visualstudio.com/) — ## **πŸ’‘ Pro Tips for Smooth Setup** βœ” Ensure MySQL Server is **running** before connecting. βœ” Always use **virtual environments** for Python projects. βœ” Use **Flask-SQLAlchemy** for an ORM-based approach. βœ” If you get connection errors, **check your MySQL username/password.** — ## **πŸ‘ Like, Share & Subscribe!** If this tutorial helped you, please **LIKE, SHARE, and SUBSCRIBE** for more **Flask, Python, and Web Development** tutorials! πŸš€ πŸ’¬ Got questions? Drop them in the **comments** below! — ### **πŸ”Ή Hashtags:** #Flask #MySQL #Python #VSCode #WebDevelopment #FlaskTutorial #Database #PythonMySQL #BackendDevelopment #SQL Start building powerful Flask apps with MySQL today! 🐍πŸ”₯