Skip to main content

Inline SQL

PyPI - Version PyPI - Python Version

A simple embedded language for running inline SQL in Python programs.

import pandas as pd
from inline_sql import sql, sql_val


def head_data(count: int) -> pd.DataFrame:
    return sql^ "SELECT * FROM 'cars.csv' LIMIT $count"


cars = head_data(50)

origin_counts = sql^ """
    SELECT origin, COUNT() FROM cars
    GROUP BY origin
    ORDER BY count DESC
"""
print(origin_counts)

most_common = origin_counts.origin[0]
print(sql_val^ """
    SELECT AVG(horsepower) FROM cars
    WHERE origin = $most_common
""")

Operations in the inline_sql library directly use an in-memory database. You can access local datasets (pandas frames), CSV files, and interpolate variables seamlessly into queries. Internally, this is implemented as a small wrapper around DuckDB.

Installation

Supports Python 3.7+, tested on all major operating systems.

pip install inline-sql

Usage

The exported sql and sql_val variables are magic objects that can be used to run queries. Queries can read from local dataframes by name, and they can embed parameters using dollar-sign notation.

>>> from inline_sql import sql, sql_val

>>> sql_val^ "SELECT 1 + 1"
2

>>> x = 5

>>> sql_val^ "SELECT 2 * $x"
10

>>> sql^ "SELECT * FROM 'disasters.csv' LIMIT 5"
                  Entity  Year   Deaths
0  All natural disasters  1900  1267360
1  All natural disasters  1901   200018
2  All natural disasters  1902    46037
3  All natural disasters  1903     6506
4  All natural disasters  1905    22758

>>> def total_deaths(entity: str) -> float:
...     return sql_val^ "SELECT SUM(deaths) FROM disasters WHERE Entity = $entity"
...

>>> total_deaths("Drought")
11731294.0

>>> total_deaths("Earthquake")
2576801.0

You can run any SQL query as described in the DuckDB documentation.

Acknowledgements

Created by Eric Zhang (@ekzhang1). Licensed under the MIT license.

Metadata

Release files for inline-sql 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for inline-sql 0.1.0
File Size Uploaded
inline_sql-0.1.0.tar.gz 22.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for inline-sql 0.1.0
File Interpreter ABI Platform
inline_sql-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 27.2 kB

Release files / inline_sql-0.1.0.tar.gz

Download URL inline_sql-0.1.0.tar.gz
Size 22.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6ded072cb73ff6297a4bae731628a95d3da59807effa9e63557946612f54734e
BLAKE2b-256 checksum
How to use checksums
a84f9454bf58d07bc8992b1ef5d0739e5c373a4eb84acf1120fbe45ffabb29e6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.0

Release files / inline_sql-0.1.0-py3-none-any.whl

Download URL inline_sql-0.1.0-py3-none-any.whl
Size 5.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7e236518919e11926cb40e2c1a63ea17a788f0f463b09dc3c1395c2243a8b6ab
BLAKE2b-256 checksum
How to use checksums
8991de5b31357d82bab25405b9c53472cca1b64f136223c4960c7c8375afd42f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via python-httpx/0.23.0

Release history Release notifications | RSS feed

0.1.2

2 release files

0.1.1

2 release files

This release

0.1.0 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page