sqlite-utils

3.39 · active · verified Mon Apr 13

sqlite-utils is a CLI tool and Python library for conveniently creating and manipulating SQLite databases. It simplifies common tasks like importing data, running queries, and managing schema. The current stable version is 3.39, with development on 4.0 in alpha. The library maintains a frequent release cadence, with over 128 releases since 2018.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a Database object, insert data into a table (which is automatically created if it doesn't exist), and then query and update data. It shows both in-memory and file-based database creation.

from sqlite_utils import Database

# Create an in-memory database
db = Database(memory=True)

# Or create a file-based database
# db = Database('my_data.db')

# Insert data into a table, creating it if it doesn't exist
db["dogs"].insert_all(
    [
        {"id": 1, "name": "Cleo", "age": 4},
        {"id": 2, "name": "Pancakes", "age": 2}
    ],
    pk="id"
)

# Query data
for row in db.query("select * from dogs where age > ?", [3]):
    print(row)

# Access table objects and perform operations
dogs_table = db["dogs"]
print(f"Total dogs: {dogs_table.count}")

dogs_table.update(1, {"age": 5})

for row in dogs_table.rows: # Iterate through all rows
    print(row)

view raw JSON →