Getting Started with SQLite in Python
Working with databases is a core part of almost any modern application. But you don't always need heavy, resource-intensive systems like MySQL or PostgreSQL. For small projects, prototypes, or local apps, Python's built-in SQLite database engine is the perfect lightweight solution.
In this guide, we'll walk through everything you need to know about using SQLite in Python. You'll learn how to perform essential database operations, including creating tables, inserting data, and running queries. We'll also cover practical examples of the UPDATE command in SQLite using Python.
What Is SQLite and Why Use It?
SQLite is a lightweight, embedded relational database engine. Unlike traditional databases, it doesn't require a separate server process. The entire database is stored in a single file on your computer's disk.
Key Benefits of SQLite:
- Built into Python's standard library via the sqlite3 module
- No server installation or configuration needed
- Perfect for local apps, mobile apps, and embedded systems
- Simple to use and fast for small to medium data volumes
- Full SQL standard support
- Cross-platform and highly portable
- Reliable and stable in production
Connecting to an SQLite Database
To work with SQLite, you use the built-in sqlite3 module. No extra installation is required — it comes pre-installed with Python.
import sqlite3
# Create or connect to a database
conn = sqlite3.connect('example.db')
# Create a cursor to execute SQL queries
cursor = conn.cursor()
Running this code creates a file called example.db in your current directory. If the file already exists, it simply connects to the existing database.
Creating Your Database Structure
Creating Tables
You create tables in SQLite using the CREATE TABLE command. Here's an example of a table for storing user information:
cursor.execute('''
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
age INTEGER,
email TEXT UNIQUE
)
''')
conn.commit()
This creates a users table with four columns:
- id — unique identifier with auto-increment
- name — user's name (required field)
- age — user's age
- email — email address (must be unique)
Creating Indexes for Performance
To speed up queries, you can create indexes on frequently searched columns:
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_users_email ON users(email)
''')
cursor.execute('''
CREATE INDEX IF NOT EXISTS idx_users_age ON users(age)
''')
conn.commit()
Working with Data
Inserting Data into a Table
To insert a single record, use the INSERT command with parameterized queries:
cursor.execute('''
INSERT INTO users (name, age, email)
VALUES (?, ?, ?)
''', ('Alice', 30, 'alice@example.com'))
conn.commit()
Bulk Data Insertion
To insert multiple records at once, use the executemany method:
users_data = [
('Bob', 25, 'bob@example.com'),
('Charlie', 35, 'charlie@example.com'),
('Diana', 28, 'diana@example.com'),
('Eve', 32, 'eve@example.com')
]
cursor.executemany('''
INSERT INTO users (name, age, email)
VALUES (?, ?, ?)
''', users_data)
conn.commit()