Shillelagh

1.4.4 · active · verified Sun Apr 12

Shillelagh is a Python library and CLI tool that enables users to query various data sources, including APIs, files, and in-memory objects, using standard SQL. It implements the Python DB API 2.0 specification based on SQLite (using the APSW library) and also provides a SQLAlchemy dialect. The library is actively maintained with frequent releases, with the current version being 1.4.4.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to Shillelagh's DB API 2.0 interface and query a public Google Sheet using SQL. Shillelagh treats the external data source (like the Google Sheet URL) as a virtual table.

import os
from shillelagh.backends.apsw.db import connect

# Example using a public Google Sheet as a data source
# For sensitive data, consider the security warning about credentials in table names.

# A public sheet with some example data
SHEET_URL = "https://docs.google.com/spreadsheets/d/1_rN3lm0R_bU3NemO0s9pbFkY5LQPcuy1pscv8ZXPtg8/edit#gid=0"

# Connect to the in-memory database
connection = connect(":memory:")
cursor = connection.cursor()

try:
    # Query the Google Sheet directly via its URL
    query = f'SELECT country, SUM(cnt) FROM "{SHEET_URL}" WHERE cnt > 0 GROUP BY country'
    print(f"Executing query:\n{query}\n")
    for row in cursor.execute(query):
        print(row)
except Exception as e:
    print(f"An error occurred: {e}")
finally:
    connection.close()

view raw JSON →