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 inparams, never in the SQL text - and identifiers are validated against a strict pattern, sofrom_("users; DROP ...")raises instead of executing. - Fluent and complete.
wherewith Django-style lookups (age__gte,id__in,deleted__isnull) or raw fragments,join/left_join,order_by(-colfor 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fd8723b3af217a33b5820627c00c8e85fb6e976c4ef6681eced0072b183337e4
|
|
| MD5 |
feba2102c7f54f094fa598fc1953546a
|
|
| BLAKE2b-256 |
cd65f1f2b100cb13d1d71b15df3aa4370e28315f42475cb2c5effe7b226a783c
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66cf0f2d4be06d18eb48ac073ff185845ed359fa2154f3f10c4ce33de9ad066f
|
|
| MD5 |
0c04c99670f86630f8ecda2b74744381
|
|
| BLAKE2b-256 |
351ca3a019dbb68ef6ce4a3f40c69b28d9839f596a1decf8a7af50d5c2cacb13
|