Sitemap
Avish Joshi

Avish Joshi

CRUD in python

2 min readDec 16, 2024

--

CRUD Operations in Python: A Beginner’s Guide

CRUD stands for Create, Read, Update, and Delete — the basic operations for managing data in any application. Whether you’re working with files, databases, or APIs, CRUD is essential. In Python, you can implement CRUD in different ways, such as working with lists, dictionaries, or databases like SQLite.

1. CRUD with Python Lists

Lists are the simplest data structure to practice CRUD operations.

Create

To add an item to a list:

data = []  # Empty list
data.append("Apple") # Adding a new item
print(data) # Output: ['Apple']

Read

To view items in a list:

for item in data:
print(item) # Output: Apple

Update

To modify an item:

data[0] = "Orange"  # Updating the first item
print(data) # Output: ['Orange']

Delete

To remove an item:

data.remove("Orange")  # Delete by value
print(data) # Output: []

2. CRUD with Python Dictionaries

Dictionaries allow key-value pairs, ideal for structured data.

Create

To add key-value pairs:

data = {}
data["name"] = "John" # Adding a new entry
print(data) # Output: {'name': 'John'}

Read

To access values:

print(data["name"])  # Output: John

Update

To change a value:

data["name"] = "Jane"  # Updating value
print(data) # Output: {'name': 'Jane'}

Delete

To remove a key-value pair:

del data["name"]
print(data) # Output: {}

3. CRUD with SQLite Database in Python

SQLite is a lightweight database built into Python, perfect for managing persistent data.

Setup SQLite

First, import the sqlite3 module and connect to a database:

import sqlite3
conn = sqlite3.connect("example.db")  # Create or connect to a database
cursor = conn.cursor() # To execute SQL queries

Create Table

To create a table for storing data:

cursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY,
name TEXT,
age INTEGER
)
""")
conn.commit()

Create (Insert Data)

To add a new record to the table:

cursor.execute("INSERT INTO users (name, age) VALUES (?, ?)", ("Alice", 25))
conn.commit()

Read (Fetch Data)

To retrieve data from the table:

cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
for row in rows:
print(row) # Output: (1, 'Alice', 25)

Update

To modify a record:

cursor.execute("UPDATE users SET age = ? WHERE name = ?", (26, "Alice"))
conn.commit()

Delete

To remove a record:

cursor.execute("DELETE FROM users WHERE name = ?", ("Alice",))
conn.commit()

Close the Connection

Always close the database connection when done:

conn.close()

Summary

CRUD operations are fundamental for working with data. Here’s a quick recap:

  • Lists: Great for basic practice of adding, reading, updating, and deleting elements.
  • Dictionaries: Perfect for structured key-value data.
  • SQLite: Use it for managing persistent data in a real-world application.

By understanding CRUD, you can build data-driven Python applications with ease! 🎉

Get Avish Joshi’s stories in your inbox

Join Medium for free to get updates from this writer.

Let me know if you’d like code examples for another database or more advanced CRUD features.

--

--