Computer Science Grade 10 20 min

Connecting Flask to a Database: Basic CRUD Operations

Learn how to connect Flask to a database and perform basic CRUD (Create, Read, Update, Delete) operations.

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define the four CRUD operations (Create, Read, Update, Delete) and their purpose in web applications. Set up a simple SQLite database and connect it to a Flask application using Flask-SQLAlchemy. Implement the 'Create' operation by processing an HTML form to add new data to the database. Implement the 'Read' operation by querying the database and displaying all records on a web page. Implement the 'Update' operation to edit an existing database record through a web interface. Implement the 'Delete' operation to remove a specific record from the database based on user input. Ever wonder how Instagram saves your photos or how your favorite game saves your high score? 🏆 It's all about talking to a database! In t...
2

Key Concepts & Vocabulary

TermDefinitionExample CRUDAn acronym for the four basic functions of persistent storage: Create, Read, Update, and Delete. These are the fundamental operations for interacting with any database.In a contacts app: Creating a new contact, Reading the list of all contacts, Updating a phone number, and Deleting a contact. DatabaseAn organized collection of structured information, or data, typically stored electronically in a computer system, allowing for efficient retrieval and management.A spreadsheet file containing a table of student names, IDs, and grades. Flask-SQLAlchemyA Flask extension that adds support for SQLAlchemy, an Object-Relational Mapper (ORM). It lets us interact with our database using Python objects and methods instead of writing raw SQL commands.Instead of writing `INSERT...
3

Core Syntax & Patterns

Defining a Database Model class ModelName(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100), nullable=False) Use this class structure to define a table in your database. Each class variable using `db.Column` becomes a column in the table, with a specified data type (like Integer or String). Creating and Saving a New Record (Create) new_record = ModelName(name='value') db.session.add(new_record) db.session.commit() To add a new row, first create an instance of your model class with the data. Then, use `db.session.add()` to stage it for saving, and `db.session.commit()` to write it permanently to the database. Querying for Records (Read) all_records = ModelName.query.all() single_record = ModelName.query.get(primary_key...

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Easy
What does the 'C' in the acronym CRUD stand for in the context of database operations?
A.Commit
B.Create
C.Column
D.Class
Easy
In Flask-SQLAlchemy, what base class must a database model inherit from to define a table structure?
A.db.Table
B.Flask.Model
C.db.Model
D.SQLAlchemy.Table
Easy
Which Flask-SQLAlchemy command is used to make changes to the database permanent, similar to saving a file?
A.db.session.add()
B.db.session.save()
C.db.session.execute()
D.db.session.commit()

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Full-Stack Web Development: Building a Simple Web Application

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.