Audrey M. Roy Greenfeld

Quietly building the future.

Print Every Row of Every SQLite Table

2024-06-19

To print every row of every table in an SQLite database:

import sqlite3

# Connect to the SQLite database
conn = sqlite3.connect("~/Library/Application Support/io.datasette.llm/logs.db")
cursor = conn.cursor()

# Get a list of all tables in the database
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()

# Loop through each table and print its rows
for table_name in tables:
    table_name = table_name[0]
    print(f"\nTable: {table_name}")

    # Select all rows from the table
    cursor.execute(f"SELECT * FROM {table_name};")
    rows = cursor.fetchall()

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

# Close the database connection
conn.close()