CRUD Operations
CRUD Operations in Python with SQLite
CRUD stands for Create, Read, Update, and Delete—the four foundational data manipulation operations powering nearly every software application and REST API. In this guide, we explore how to perform each operation efficiently using Python's sqlite3 module.
1. Setting Up Our Sample Schema
Let's work with an e-commerce products table:
2. CREATE: Single and Bulk Inserts
Single Insert (cursor.lastrowid)
When inserting a single record, cursor.lastrowid retrieves the auto-generated primary key:
High-Performance Bulk Inserts with executemany()
Avoid looping over single execute() statements for thousands of records. Calling cursor.executemany() executes the batch within a single optimized transaction:
3. READ: Querying, Filtering, and Aggregation
Parameterized Filtering and Sorting
SQL Aggregations
4. UPDATE: Modifying Existing Records
WHERE clause! Omitting WHERE updates every single row in the entire table!Check cursor.rowcount to verify how many rows were modified:
5. DELETE: Removing Records (Hard vs. Soft Deletes)
Hard Delete (Permanent Removal)
Soft Delete Pattern (Industry Best Practice)
Instead of permanently wiping records from disk, enterprise applications add an is_active or deleted_at column:
Multiple Choice Questions
1. Which method on a cursor should you use to insert a list of 500 records in a single batch?
A. cursor.execute() in a loop B. cursor.executemany() C. cursor.insert_batch() D. cursor.bulk_write() Answer: B Explanation: cursor.executemany() executes a parameterized SQL command against all parameter sequences in a single optimized pass.
2. How can you retrieve the auto-generated primary key of the most recently inserted row?
A. cursor.get_id() B. cursor.lastrowid C. cursor.fetchone()[0] D. connection.primary_key Answer: B Explanation: cursor.lastrowid contains the rowid or auto-incremented primary key of the last inserted record.
3. What cursor attribute indicates the number of rows affected by an UPDATE or DELETE statement?
A. cursor.modified B. cursor.rowcount C. cursor.affected_rows D. cursor.total_changes Answer: B Explanation: cursor.rowcount reflects the number of records altered by the most recent executing statement.
4. What critical mistake happens if an UPDATE statement is executed without a WHERE clause?
A. A SyntaxError is raised B. The first row in the table is updated C. Every single row in the entire table is updated with the new value D. Nothing happens Answer: C Explanation: SQL applies statements globally unless constrained by a WHERE filter; omitting WHERE modifies all records in the table.
5. What is the primary difference between a "Hard Delete" and a "Soft Delete"?
A. Hard deletes require passwords B. Hard deletes permanently remove rows from the table, while soft deletes flag records as inactive via a status column C. Soft deletes only work in memory D. Hard deletes only run on Linux Answer: B Explanation: Hard deletion removes records completely from disk, whereas soft deletion updates a flag (e.g. is_active = 0), preserving historical audit trails.
Project: Student Records Database
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Connecting Python with SQLite | Project: Student Records Database |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.