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', '[email protected]');
```
---
### **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!