[Python] Basics of Retrieving Data with SQLite3: Cursor Loop vs. fetchall vs. fetchone

When retrieving data from a database using Python’s standard sqlite3 module, there are several methods available. This article explains how to directly iterate over the cursor object, how to retrieve everything at once using fetchall(), and how to retrieve data one row at a time using fetchone().

We will demonstrate these methods using a practical Library Management System scenario.

目次

Executable Sample Code

The following code is a complete script that creates a database, inserts sample book data, and retrieves that data using three different patterns. You can copy and paste this directly into your environment to test it.

import sqlite3
import os

# Database file name
DB_FILE = "library.db"

def initialize_database():
    """Function to create data for verification"""
    # Delete existing file if it exists (initialization)
    if os.path.exists(DB_FILE):
        os.remove(DB_FILE)

    with sqlite3.connect(DB_FILE) as conn:
        cursor = conn.cursor()
        
        # Create table
        cursor.execute("""
            CREATE TABLE books (
                book_id INTEGER PRIMARY KEY,
                title TEXT,
                author TEXT
            )
        """)
        
        # Insert sample data
        data = [
            (1, 'The Great Gatsby', 'F. Scott Fitzgerald'),
            (2, '1984', 'George Orwell'),
            (3, 'To Kill a Mockingbird', 'Harper Lee'),
            (4, 'Pride and Prejudice', 'Jane Austen')
        ]
        cursor.executemany("INSERT INTO books VALUES (?, ?, ?)", data)
        conn.commit()

def main():
    # Initialize the database
    initialize_database()

    print("=== Data Retrieval Started ===")

    with sqlite3.connect(DB_FILE) as conn:
        cursor = conn.cursor()

        # Pattern 1: Direct Loop on Cursor Object
        # Efficient for memory, suitable for processing large amounts of data
        print("\n--- 1. Cursor Loop ---")
        cursor.execute("SELECT * FROM books")
        
        for record in cursor:
            # Access the entire tuple or specific columns (e.g., Title)
            print(f"Record: {record}, Title: {record[1]}")

        # Pattern 2: Retrieve all with fetchall()
        # Loads results into memory as a list; suitable when you need list operations
        print("\n--- 2. fetchall() ---")
        cursor.execute("SELECT * FROM books")
        all_rows = cursor.fetchall()
        
        for row in all_rows:
            print(row)

        # Pattern 3: Retrieve one by one with fetchone()
        # Used when you only need a specific row or want to control the loop manually
        print("\n--- 3. fetchone() ---")
        cursor.execute("SELECT * FROM books")
        
        # Get the 1st row
        first_row = cursor.fetchone()
        print(f"1st: {first_row}")
        
        # Get the 2nd row (Cursor moves forward, so the next row is retrieved)
        second_row = cursor.fetchone()
        print(f"2nd: {second_row}")

if __name__ == "__main__":
    main()

Explanation of Retrieval Methods

1. Iterating Over the Cursor Object

This method involves running a for loop directly on the cursor object after executing cursor.execute().

  • Feature: Reads data one row at a time, keeping memory consumption low.
  • Use Case: Recommended when processing large datasets sequentially (e.g., tens of thousands of rows).

2. fetchall() Method

This method retrieves all rows from the search results at once as a list (list of tuples).

  • Feature: All data is loaded into memory. This is convenient if you want to access data by index or use list methods after retrieval.
  • Note: If the amount of data is extremely large, it may cause a MemoryError.

3. fetchone() Method

This method retrieves only the next single row from the search results. The cursor position advances each time it is executed. It returns None when there is no more data.

  • Feature: Allows you to pinpoint and retrieve only the necessary data.
  • Use Case: Used when you want to “search by ID and get only one record” or when you want to process data using a custom while loop condition.
よかったらシェアしてね!
  • URLをコピーしました!
  • URLをコピーしました!

この記事を書いた人

私が勉強したこと、実践したこと、してることを書いているブログです。
主に資産運用について書いていたのですが、
最近はプログラミングに興味があるので、今はそればっかりです。

目次