Last modified: Sep 22, 2026

Read SQLite Database in Python

Reading an SQLite database in Python is a common task for developers working with lightweight data storage. Python provides a built-in module called sqlite3 that allows you to interact with SQLite databases seamlessly. This guide will walk you through the process of reading data from an SQLite database using Python.

Prerequisites

Before diving into reading SQLite databases, ensure that you have Python installed on your system. The sqlite3 module comes pre-installed with Python 3, so no additional installation steps are required. If you're using an older version of Python, consider upgrading to leverage the full capabilities of the sqlite3 module.

Connecting to an SQLite Database

The first step in reading an SQLite database is establishing a connection to it. You can do this using the connect() function provided by the sqlite3 module. Here's how:


import sqlite3

# Connect to the SQLite database (or create it if it doesn't exist)
conn = sqlite3.connect('example.db')

In the example above, we import the sqlite3 module and use the connect() function to establish a connection to a database file named example.db. If the file does not exist, SQLite will create a new one.

Creating a Cursor Object

Once connected, you need to create a cursor object. The cursor allows you to execute SQL commands and fetch results. Use the cursor() method to create a cursor:


# Create a cursor object
cursor = conn.cursor()

Executing SQL Queries

With the cursor object, you can execute SQL queries to read data from the database. For instance, if you have a table named users, you can retrieve all records using the execute() method:


# Execute a SELECT query to retrieve all rows from the users table
cursor.execute("SELECT * FROM users")

Fetching Results

After executing a query, you can retrieve the results using the cursor's fetching methods. The most commonly used methods are fetchall(), fetchone(), and fetchmany().

Using fetchall()

The fetchall() method retrieves all remaining rows of a query result. It returns a list of tuples where each tuple represents a row:


# Fetch all rows from the executed query
rows = cursor.fetchall()

# Print each row
for row in rows:
    print(row)

Sample output might look like this:


(1, 'Alice', '[email protected]')
(2, 'Bob', '[email protected]')
(3, 'Charlie', '[email protected]')

Using fetchone()

The fetchone() method retrieves the next row of a query result. It's useful when you want to process rows one at a time. For more details on retrieving single rows, check out our guide on Fetchone Python SQLite: Retrieve Single Rows.


# Fetch the first row
row = cursor.fetchone()
print(row)

Output:


(1, 'Alice', '[email protected]')

Using fetchmany()

The fetchmany() method retrieves a specified number of rows. By default, it returns one row, but you can specify the number of rows:


# Fetch two rows
rows = cursor.fetchmany(2)
print(rows)

Output:


[(2, 'Bob', '[email protected]'), (3, 'Charlie', '[email protected]')]

Filtering Data with WHERE Clause

You can filter data by adding a WHERE clause to your SQL query. This is useful for retrieving specific records:


# Execute a filtered query
cursor.execute("SELECT * FROM users WHERE age > 25")

# Fetch and print results
rows = cursor.fetchall()
for row in rows:
    print(row)

Reading Specific Columns

Instead of selecting all columns, you can specify the columns you want to retrieve:


# Retrieve only the name and email columns
cursor.execute("SELECT name, email FROM users")

rows = cursor.fetchall()
for row in rows:
    print(row)

Output:


('Alice', '[email protected]')
('Bob', '[email protected]')
('Charlie', '[email protected]')

Handling Errors and Closing Connections

It's important to handle potential errors and close the database connection after completing your operations. Use a try-except-finally block to manage resources effectively:


try:
    conn = sqlite3.connect('example.db')
    cursor = conn.cursor()
    cursor.execute("SELECT * FROM users")
    rows = cursor.fetchall()
    for row in rows:
        print(row)
except sqlite3.Error as e:
    print(f"An error occurred: {e}")
finally:
    if conn:
        conn.close()

Conclusion

Reading data from an SQLite database in Python is straightforward once you understand the basic steps: connect to the database, create a cursor, execute queries, and fetch results. The sqlite3 module provides powerful tools like fetchall(), fetchone(), and fetchmany() to help you retrieve data efficiently. For more advanced operations, explore topics like Python SQLite API Guide: Build Database Apps or Python SQLite Create Database Guide. With these skills, you can start building robust applications that interact with SQLite databases confidently.