CRUD Operations with SQLite3 in Python: A Comprehensive Guide

Ajay Parmar
3 min readJun 18, 2024

--

Image by DALL-E 3

CRUD Operations with SQLite3 in Python: A Comprehensive Guide

CRUD operations form the backbone of any data management system. CRUD stands for Create, Read, Update, and Delete — four fundamental operations that are essential for interacting with databases. In this blog, we will explore how to perform these operations using SQLite3 in Python.

Table of Contents

  1. Introduction to CRUD Operations
  2. Setting Up SQLite3 in Python
  3. Creating a Database and Table
  4. Inserting Data (Create)
  5. Fetching Data (Read)
  6. Updating Data (Update)
  7. Deleting Data (Delete)
  8. Conclusion

1. Introduction to CRUD Operations

CRUD operations are basic functions that allow for interaction with databases:

  • Create: Add new records to a database.
  • Read: Retrieve data from a database.
  • Update: Modify existing data in a database.
  • Delete: Remove data from a database.

2. Setting Up SQLite3 in Python

Before diving into CRUD operations, ensure that Python is installed on your machine. You can check this by running:

bash
Copy code
python --version

Since sqlite3 is included in Python's standard library, no additional installation is required. Import the module in your Python script as follows:

python
Copy code
import sqlite3

3. Creating a Database and Table

To create a database and a table, follow these steps:

  1. Connect to a Database: If the database does not exist, SQLite3 will create it.
python
Copy code
conn = sqlite3.connect('example.db')
cur = conn.cursor()
  1. Create a Table: Define the table schema using an SQL statement.
python
Copy code
cur.execute('''
CREATE TABLE IF NOT EXISTS students (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
age INTEGER,
grade TEXT
)
''')
conn.commit()

4. Inserting Data (Create)

To insert data into the table, use the INSERT INTO SQL command:

python
Copy code
cur.execute('''
INSERT INTO students (name, age, grade)
VALUES (?, ?, ?)
''', ('Alice', 21, 'A'))
conn.commit()

To insert multiple records, use executemany:

python
Copy code
students = [
('Bob', 22, 'B'),
('Charlie', 23, 'C'),
('Dave', 24, 'D')
]
cur.executemany('''
INSERT INTO students (name, age, grade)
VALUES (?, ?, ?)
''', students)
conn.commit()

5. Fetching Data (Read)

To read data from the table, use the SELECT SQL command:

python
Copy code
cur.execute('SELECT * FROM students')
rows = cur.fetchall()
for row in rows:
print(row)

To fetch data with specific criteria, use SELECT with WHERE:

python
Copy code
cur.execute('SELECT * FROM students WHERE age >= 22')
rows = cur.fetchall()
for row in rows:
print(row)

6. Updating Data (Update)

To update existing data, use the UPDATE SQL command:

python
Copy code
cur.execute('''
UPDATE students
SET grade = ?
WHERE name = ?
''', ('A+', 'Bob'))
conn.commit()

7. Deleting Data (Delete)

To delete data from the table, use the DELETE SQL command:

python
Copy code
cur.execute('''
DELETE FROM students
WHERE name = ?
''', ('Charlie',))
conn.commit()

8. Conclusion

CRUD operations are essential for managing data in any application. SQLite3, with its lightweight and self-contained nature, paired with Python’s simplicity, provides a powerful toolset for handling these operations efficiently. Whether you are developing small to medium-sized applications, SQLite3’s integration with Python can help you manage data effectively.

By following this guide, you can set up an SQLite3 database in Python and perform basic CRUD operations, ensuring that your application can create, read, update, and delete data as needed.

--

--