Simple CRUD with SQLite and sqlite3 Module

Ever Wish Your Python Programs Could Remember Things? Let’s Build a Database!

Hey there! Ever felt frustrated because your Python programs only hold data while they’re running? Imagine building a to-do list app that disappears when you close it – frustrating, right? That’s where databases shine! And believe it or not, building simple, powerful databases in Python using the sqlite3 module is surprisingly straightforward. We’re going to explore the world of Simple CRUD operations with SQLite and the sqlite3 module today, and I bet you’ll be amazed how easy it is.

Core Concepts: CRUD and SQLite Explained

Core Concepts: CRUD and SQLite Explained “Core Concepts: CRUD and SQLite Explained”)

Let’s break down the jargon. “CRUD” stands for Create, Read, Update, and Delete – the four basic operations you’ll perform on data in almost any database. Think of it like managing a super-organized filing cabinet: you can add new files (Create), look up files (Read), change information in existing files (Update), and toss out files you no longer need (Delete).

SQLite is a lightweight, file-based database engine. This means your database is stored as a single file on your computer – no complicated server setups required! The sqlite3 module is Python’s built-in library for interacting with SQLite databases. It’s like a handy tool that lets your Python program talk to this file-based database.

3 Simple Projects/Applications: Let’s Get Our Hands Dirty!

3 Simple Projects/Applications:  Let's Get Our Hands Dirty! “3 Simple Projects/Applications: Let’s Get Our Hands Dirty!”)

Let’s build some tiny applications to solidify our understanding. I’ll walk you through each step, and you can copy-paste the code into your own Python interpreter and run it.

Project 1: A Simple Contact List

This project will let us create, read, update, and delete contact information stored in an SQLite database.

import sqlite3

# Connect to the database (or create it if it doesn't exist)
conn = sqlite3.connect('contacts.db') # This creates a file named 'contacts.db' in your current directory
cursor = conn.cursor() # 'cursor' is our tool to interact with the db

# Create a table to store contact information
cursor.execute('''
    CREATE TABLE IF NOT EXISTS contacts (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT,
        phone TEXT
    )
''') # This creates a table if one doesn't exist already, otherwise it does nothing

# Insert some initial contacts
cursor.execute("INSERT INTO contacts (name, phone) VALUES (?, ?)", ('Alice', '555-1212'))
cursor.execute("INSERT INTO contacts (name, phone) VALUES (?, ?)", ('Bob', '555-3434'))

# Commit the changes to the database
conn.commit()


# Read all contacts
cursor.execute("SELECT * FROM contacts")
contacts = cursor.fetchall()
for contact in contacts:
    print(contact)

#Update a contact
cursor.execute("UPDATE contacts SET phone = ? WHERE name = ?", ('555-9999', 'Alice'))
conn.commit()


#Read contacts after update
cursor.execute("SELECT * FROM contacts")
contacts = cursor.fetchall()
for contact in contacts:
    print(contact)

#Delete a contact
cursor.execute("DELETE FROM contacts WHERE name = ?", ('Bob',))
conn.commit()

# Close the database connection
conn.close()

This code first establishes a connection to the database. Then, it creates a table named “contacts” with columns for ID, name, and phone. It then inserts two contacts, reads all contacts, updates Alice’s phone number and then deletes Bob. Finally it closes the connection.

Project 2: A Simple Task Manager

import sqlite3

conn = sqlite3.connect('tasks.db')
cursor = conn.cursor()

cursor.execute('''
    CREATE TABLE IF NOT EXISTS tasks (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        description TEXT,
        completed BOOLEAN
    )
''')

cursor.execute("INSERT INTO tasks (description, completed) VALUES (?, ?)", ('Buy groceries', 0))
conn.commit()

# ... (Add more CRUD operations similar to the contact list example)

conn.close()

This is very similar to the contact list, but manages tasks instead. Notice the use of a BOOLEAN type to track task completion.

Project 3: Storing Simple Notes

import sqlite3

conn = sqlite3.connect('notes.db')
cursor = conn.cursor()

cursor.execute('''
    CREATE TABLE IF NOT EXISTS notes (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        note TEXT
    )
''')

# ... (Add CRUD operations for adding, reading, updating, and deleting notes)

conn.close()

This project shows how to store textual notes. The core CRUD principles remain the same, just applied to a different data structure.

Summary: You’ve Got This!

Summary: You've Got This! “Summary: You’ve Got This!”)

You’ve just taken your first steps into the world of database programming using Python’s sqlite3 module! You’ve learned about CRUD operations, explored SQLite’s simplicity, and even built three mini-applications. This is a fantastic foundation for more advanced database work. Remember, practice is key! The more you experiment with these examples and try to build your own projects, the more confident and skilled you’ll become.

If you ever get stuck, or want to tackle more complex projects involving SQLite, Python database management, or even explore other database systems like PostgreSQL https://www.postgresql.org/ or MySQL https://www.mysql.com/, don’t hesitate to reach out! We’re here to help you every step of the way, transforming your ideas into reality. We love collaborating and partnering with you to turn your complex database projects into elegant, working solutions.


⬅️ Previous Post: Understanding HTTP Methods and Status Codes

Explore Our Series on This Topic:



Need Help with a Python Assignment or Project?

Learning Python is exciting — but it can also get tricky sometimes. Whether you're stuck on a bug, running out of time on an assignment, or building something cool and just need a little help...

We’ve got your back. 💪

Our team is here to support you with:

  • ✅ Python assignments & school projects
  • ✅ Debugging errors or fixing code
  • ✅ Custom scripts or mini tools
  • ✅ Personal coding challenges or portfolio projects

Don’t struggle alone — reach out and let us help you get it done the smart way.

📩 Click here to contact us or

Let’s build something awesome together! Contact Us Now!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top