- Python for Web Development
-
Overview
- Introduction to Flask and Django
- Setting Up a Flask Application
- Django Models and Migrations
- Routing and URL Handling in Django and Flask
- Forms and User Authentication in Django and Flask
- REST API Development with Flask & Django
- Working with Databases (SQLite, PostgreSQL, MySQL)
- Template Engines (Jinja2 for Flask, Django Templates)
- Deployment of Flask & Django Applications on AWS, GCP, and Heroku
- Security Best Practices for Web Apps
Working with Databases (SQLite, PostgreSQL, MySQL)
Add to BookmarkDatabases are the backbone of web applications, storing and managing data efficiently. In this tutorial, we’ll explore how to use SQLite, PostgreSQL, and MySQL with Flask and Django.
1. Databases in Django
Django comes with built-in ORM (Object-Relational Mapping), which makes it easy to interact with databases. By default, Django uses SQLite, but it also supports PostgreSQL and MySQL.
1.1 Configuring the Database in Django
Using SQLite (Default Database)
Django uses SQLite by default. The configuration in settings.py looks like this:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / "db.sqlite3",
}
}No extra setup needed. SQLite is great for small projects and local development.
Using PostgreSQL in Django
1. Install the PostgreSQL adapter for Python:
pip install psycopg2-binary2. Update settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'your_database',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '5432',
}
}3. Apply migrations and create a superuser:
python manage.py migrate
python manage.py createsuperuserPostgreSQL is great for large-scale applications needing advanced features like JSONB and full-text search.
Using MySQL in Django
1. Install the MySQL adapter for Python:
pip install mysqlclient2. Update settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'your_database',
'USER': 'your_user',
'PASSWORD': 'your_password',
'HOST': 'localhost',
'PORT': '3306',
}
}3. Apply migrations:
python manage.py migrateMySQL is best for applications that require speed and reliability, such as e-commerce sites.
2. Databases in Flask
Flask is more flexible than Django but requires additional setup for databases.
Using SQLite in Flask
1. Install SQLite support:
pip install flask_sqlalchemy2. Configure the database in app.py:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db'
db = SQLAlchemy(app)3. Define a model:
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), nullable=False)4. Create the database:
python
>>> from app import db
>>> db.create_all()SQLite is great for small apps or local development.
Using PostgreSQL in Flask
1. Install PostgreSQL adapter:
pip install psycopg2-binary2. Configure app.py:
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://your_user:your_password@localhost/your_database'3. Apply the same steps as SQLite to create the database.
PostgreSQL is useful for large applications needing scalability and complex queries.
Using MySQL in Flask
1. Install MySQL adapter:
pip install pymysql2. Configure app.py:
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://your_user:your_password@localhost/your_database'3. Apply the same steps as above.
MySQL is ideal for performance-driven applications.
3. Django vs Flask Database Handling
| Feature | Django (ORM) | Flask (SQLAlchemy) |
|---|---|---|
| Ease of Use | Easier, built-in ORM | More flexible, requires setup |
| Performance | Optimized for Django | Faster for lightweight apps |
| Best for | Full-stack Django apps | Microservices, APIs |
Django's ORM is easier for beginners.
Flask with SQLAlchemy gives more control over queries.
Conclusion
We explored how to use SQLite, PostgreSQL, and MySQL in both Django and Flask.
- SQLite is great for small projects.
- PostgreSQL is best for large-scale applications.
- MySQL is ideal for high-performance applications.
Prepare for Interview
- JavaScript Interview Questions for 5+ Years Experience
- JavaScript Interview Questions for 2–5 Years Experience
- JavaScript Interview Questions for 1–2 Years Experience
- JavaScript Interview Questions for 0–1 Year Experience
- JavaScript Interview Questions For Fresher
- SQL Interview Questions for 5+ Years Experience
- SQL Interview Questions for 2–5 Years Experience
- SQL Interview Questions for 1–2 Years Experience
- SQL Interview Questions for 0–1 Year Experience
- SQL Interview Questions for Freshers
- Design Patterns in Python
- Dynamic Programming and Recursion in Python
- Trees and Graphs in Python
- Linked Lists, Stacks, and Queues in Python
- Sorting and Searching in Python
Random Blogs
- Variable Assignment in Python
- Where to Find Free Datasets for Your Next Machine Learning & Data Science Project
- How to Install Tableau and Power BI on Ubuntu Using VirtualBox
- Mastering SQL in 2025: A Complete Roadmap for Beginners
- The Ultimate Guide to Machine Learning (ML) for Beginners
- Navigating AI Careers in 2025: Data Science, Machine Learning, Deep Learning, and More
- Python Challenging Programming Exercises Part 2
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
- The Ultimate Guide to Data Science: Everything You Need to Know
- AI & Space Exploration – AI’s Role in Deep Space Missions and Planetary Research
- The Ultimate Guide to Artificial Intelligence (AI) for Beginners
- Loan Default Prediction Project Using Machine Learning
- Google’s Core Update in May 2020: What You Need to Know
- Python Challenging Programming Exercises Part 3
- String Operations in Python
Datasets for Machine Learning
- Awesome-ChatGPT-Prompts
- Amazon Product Reviews Dataset
- Ozone Level Detection Dataset
- Bank Transaction Fraud Detection
- YouTube Trending Video Dataset (updated daily)
- Covid-19 Case Surveillance Public Use Dataset
- US Election 2020
- Forest Fires Dataset
- Mobile Robots Dataset
- Safety Helmet Detection
- All Space Missions from 1957
- OSIC Pulmonary Fibrosis Progression Dataset
- Wine Quality Dataset
- Google Audio Dataset
- Iris flower dataset


