Skip to main content

larzsql

An injection-safe SQL query builder. Pure Python, zero dependencies.

Concatenating SQL is how injection happens. larzsql builds SELECT/INSERT/UPDATE/ DELETE where every value becomes a bound parameter (never interpolated) and every table/column name is validated as a plain identifier - so a value or a name can't smuggle in SQL. You get back (sql, params) ready for your DB-API driver.

from larzsql import select, insert, update, delete

select("id", "name").from_("users").where(active=True, age__gte=18) \
    .order_by("-created").limit(10).build()
# ('SELECT id, name FROM users WHERE active = ? AND age >= ? ORDER BY created DESC LIMIT 10',
#  [True, 18])

insert("users").values(name="Ada", email="a@b.com").build()
# ('INSERT INTO users (name, email) VALUES (?, ?)', ['Ada', 'a@b.com'])

Why

  • Injection-safe by construction. Values are always bound parameters - even a "1; DROP TABLE users; --" value ends up in params, never in the SQL text - and identifiers are validated against a strict pattern, so from_("users; DROP ...") raises instead of executing.
  • Fluent and complete. where with Django-style lookups (age__gte, id__in, deleted__isnull) or raw fragments, join/left_join, order_by (-col for DESC), limit/offset, plus INSERT/UPDATE/DELETE.
  • Driver-agnostic. build(paramstyle="?") for sqlite or "%s" for psycopg2/MySQL.
  • Not an ORM. It builds strings + params; you run them. Zero dependencies, pairs with larzdb / larzmigrate.

Install

pip install larzsql

Usage

from larzsql import select, insert, update, delete

select("*").from_("orders").where(status__in=["paid", "shipped"]).build()
select("u.id", "o.total").from_("users").left_join("orders", "orders.user_id = users.id").build()
update("users").set(active=False).where(id=5).build()
delete("sessions").where(expires__lt=now).build()

sql, params = select("*").from_("t").where(id=1).build(paramstyle="%s")
cursor.execute(sql, params)

Tests

python -m unittest discover -s tests -v   # 17 tests incl. injection safety

The Larz stack

One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.

License

MIT (c) larz-scripter

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

larzsql-0.1.0.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

larzsql-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file larzsql-0.1.0.tar.gz.

File metadata

  • Download URL: larzsql-0.1.0.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzsql-0.1.0.tar.gz
Algorithm Hash digest
SHA256 fd8723b3af217a33b5820627c00c8e85fb6e976c4ef6681eced0072b183337e4
MD5 feba2102c7f54f094fa598fc1953546a
BLAKE2b-256 cd65f1f2b100cb13d1d71b15df3aa4370e28315f42475cb2c5effe7b226a783c

See more details on using hashes here.

File details

Details for the file larzsql-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: larzsql-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.13

File hashes

Hashes for larzsql-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 66cf0f2d4be06d18eb48ac073ff185845ed359fa2154f3f10c4ce33de9ad066f
MD5 0c04c99670f86630f8ecda2b74744381
BLAKE2b-256 351ca3a019dbb68ef6ce4a3f40c69b28d9839f596a1decf8a7af50d5c2cacb13

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 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