50/90: Learn Core Python in 90 Days: A Beginner’s Guide
Day 50: Working with Databases in Flask (Part 2): CRUD Operations
Welcome to Day 50 of our 90-day journey to learn core Python! Today marks the halfway point of our adventure, and what better way to celebrate than by diving deeper into working with databases in Flask. In this post, we’ll explore the essential CRUD operations: Create, Read, Update, and Delete, enabling us to manage data effectively in our web applications.
Review of Flask-SQLAlchemy
In a previous blog post, we introduced Flask-SQLAlchemy for database integration. As a quick recap, Flask-SQLAlchemy simplifies database operations by allowing you to work with databases using Python objects and classes.
CRUD Operations with Flask-SQLAlchemy
Let’s explore each CRUD operation and how to implement it using Flask-SQLAlchemy:
1. Create © — Adding Data
To create a new record in the database, you’ll create an instance of your SQLAlchemy model and add it to the session. Here’s an example:
from your_app import db, User
# Create a new user
new_user = User(username='john_doe', email='john@example.com')
db.session.add(new_user)
db.session.commit()
2. Read (R) — Retrieving Data
To retrieve data from the database, you can use SQLAlchemy queries. For instance, to retrieve all users, you can do the following:
all_users = User.query.all()
To retrieve a specific user by their ID:
user = User.query.get(1) # Replace 1 with the desired user ID
3. Update (U) — Modifying Data
Updating data involves loading a record, making changes, and then committing those changes to the database. Here’s an example of updating a user’s email:
user = User.query.get(1) # Load the user with ID 1
user.email = 'new_email@example.com' # Update the email
db.session.commit()
4. Delete (D) — Removing Data
To delete a record, you’ll load it and then call the delete
method followed by a commit. For instance, to delete a user by their ID:
user = User.query.get(1) # Load the user with ID 1
db.session.delete(user)
db.session.commit()
Real-World Applications
CRUD operations are the foundation of database interactions in web applications. Whether you’re managing user profiles, blog posts, or product listings, understanding how to create, read, update, and delete data is crucial.
Conclusion
Congratulations on reaching Day 50 of our Python learning journey! Today, we explored the essential CRUD operations in Flask-SQLAlchemy: Create, Read, Update, and Delete. We learned how to perform these operations to manage data effectively in our Flask applications.
Take some time to practice CRUD operations in your Flask projects. As we continue our journey, we’ll explore more advanced database concepts and dive deeper into building robust web applications.
Keep up the great work, and let’s continue building powerful web applications with Python and Flask! 🚀
Note: This blog post is part of a 90-day series to teach core Python programming from scratch. You can find all previous days in the series index here.