Fastlite: Enhanced SQLite Usability

0.2.4 · active · verified Sun Apr 12

Fastlite provides quality-of-life improvements for interactive use of the `sqlite-utils` library, particularly in Jupyter environments. It enhances database and table access with auto-completion, simplifies dataclass creation from schemas, and streamlines data manipulation. The library is currently active, with version 0.2.4 addressing recent bug fixes and continuing to evolve with new usability features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize an in-memory SQLite database, create a table with a primary key, insert new records, and retrieve data. It covers basic table creation, insertion, and querying using Fastlite's simplified `db.t` table access.

from fastlite import database
import os

# Create an in-memory database for quick testing
db = database(':memory:')

# Or, for a persistent file-based database
# db_file = 'example.db'
# if os.path.exists(db_file): os.remove(db_file) # Clean up for repeatable example
# db = database(db_file)

# Create a table named 'users'
users_table = db.t.users
users_table.create(
    id=int, 
    name=str, 
    email=str,
    pk='id' # Set 'id' as the primary key
)

# Insert data into the 'users' table
users_table.insert({'id': 1, 'name': 'Alice', 'email': 'alice@example.com'})
users_table.insert({'id': 2, 'name': 'Bob', 'email': 'bob@example.com'})

# Query all users
print('All users:')
for user in users_table():
    print(user)

# Query a specific user by primary key
alice = users_table.get(1)
print(f'\nUser with ID 1: {alice}')

# Update a user
users_table.update({'id': 1, 'name': 'Alicia'})
print('\nUpdated user with ID 1:')
for user in users_table(id=1):
    print(user)

view raw JSON →