- 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-binary
2. 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 createsuperuser
PostgreSQL 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 mysqlclient
2. 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 migrate
MySQL 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_sqlalchemy
2. 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-binary
2. 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 pymysql
2. 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
- 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
- Debugging in Python
- Unit Testing in Python
- Asynchronous Programming in PYthon
- Multithreading and Multiprocessing in Python
- Context Managers in Python
- Decorators in Python
Random Blogs
- 15 Amazing Keyword Research Tools You Should Explore
- Understanding HTAP Databases: Bridging Transactions and Analytics
- Store Data Into CSV File Using Python Tkinter GUI Library
- Top 15 Recommended SEO Tools
- OLTP vs. OLAP Databases: Advanced Insights and Query Optimization Techniques
- Grow your business with Facebook Marketing
- How AI Companies Are Making Humans Fools and Exploiting Their Data
- Top 10 Knowledge for Machine Learning & Data Science Students
- 5 Ways Use Jupyter Notebook Online Free of Cost
- Python Challenging Programming Exercises Part 2
- Datasets for analyze in Tableau
- Create Virtual Host for Nginx on Ubuntu (For Yii2 Basic & Advanced Templates)
- Important Mistakes to Avoid While Advertising on Facebook
- Avoiding the Beginner’s Trap: Key Python Fundamentals You Shouldn't Skip
- Internet of Things (IoT) & AI – Smart Devices and AI Working Together
Datasets for Machine Learning
- 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
- Artificial Characters Dataset