Python Dataset Library (SQL Toolkit)

1.6.2 · active · verified Sat Apr 11

The Python 'dataset' library (version 1.6.2) is a lightweight toolkit for simplified Python-based database access, abstracting away much of the direct SQL interaction. It enables reading and writing data in SQL data stores with an API designed to feel as straightforward as working with JSON files, offering features like implicit table and column creation, upserts, and convenient query helpers. Built on SQLAlchemy, it ensures compatibility with major databases such as SQLite, PostgreSQL, and MySQL. The library maintains a steady release cadence with bug fixes and feature enhancements.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a database (using an in-memory SQLite for simplicity), create a table implicitly, insert and update data, and query records using the `dataset` library.

import dataset
import os

# Connect to an in-memory SQLite database
db = dataset.connect('sqlite:///:memory:')

# Get a table reference; it will be created if it doesn't exist
table = db['users']

# Insert new records; columns are created automatically
table.insert(dict(name='John Doe', age=30, city='New York'))
table.insert(dict(name='Jane Smith', age=25, city='London'))

# Update an existing record
table.update(dict(name='John Doe', age=31), ['name'])

# Find all records
all_users = table.all()
print("All users:")
for user in all_users:
    print(user)

# Find one record by a specific field
john = table.find_one(name='John Doe')
print(f"\nJohn Doe's updated age: {john['age']}")

# Find records with a filter
london_users = table.find(city='London')
print("\nUsers in London:")
for user in london_users:
    print(user)

# Using environment variable for database URL (example)
# os.environ['DATABASE_URL'] = 'sqlite:///mydb.db'
# db_env = dataset.connect()
# print(f"\nConnected via env var to: {db_env.url}")

view raw JSON →