python-sql
Library to write SQL queries
What it is and what it does
python-sql is a query builder that lets you write SQL statements as Python objects instead of raw strings. You define tables, columns, and conditions using Python syntax—like `user.select(user.name)` or `user.where(user.active == True)`—and the library generates parameterized SQL and a tuple of bind parameters. This approach prevents SQL injection by separating query structure from values, and makes complex queries (joins, subselects, aggregations, updates, deletes) easier to construct and read.
The library supports SELECT, INSERT, UPDATE, and DELETE operations, with features like joins, GROUP BY, ORDER BY, aggregates, and subqueries. It also provides a Flavor system to adapt output to different SQL dialects (limit/offset styles, parameter markers like `?` or `:0`). With no external dependencies and pure Python implementation, it integrates easily into any project that needs to build queries dynamically without an ORM.
Use it for:
- Build SELECT queries with WHERE, JOIN, GROUP BY, and ORDER BY clauses programmatically without string concatenation.
- Generate INSERT, UPDATE, and DELETE statements with parameterized values to prevent SQL injection.
- Construct complex subqueries and nested conditions using Python operators and method chaining.
- Adapt query output to different SQL dialects (PostgreSQL, MySQL, Oracle, SQLite) via Flavor configuration.
- Generate parameterized queries for use with database drivers that require separate query strings and parameter tuples.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Constructs SQL queries programmatically using Python syntax instead of string concatenation, generating parameterized SQL and parameter tuples for safe database execution.
Yes. python-sql is a lightweight, dependency-free query builder suitable for projects that need programmatic SQL construction without a full ORM. It's actively maintained, production-stable, has no known vulnerabilities, and works with Python 3.9+. Install it if you're building dynamic queries and want type-safe, injection-proof SQL generation; skip it if you prefer raw SQL strings or a heavier ORM like SQLAlchemy.
Install
python-sql on PyPI
pip
pip install python-sqluv
uv add python-sqlpoetry
poetry add python-sqlInstalling python-sql
Before you install
Low friction: pure Python wheel with no runtime dependencies. Actively maintained with a release 133 days ago and production-stable status.
License in practice
BSD-3-Clause permissive license allows commercial and private use with minimal restrictions; attribution required.
Quickstart
pip install python-sql
from sql import *
user = Table('user')
select = user.select(user.name)
query_string, params = tuple(select)
# ('SELECT "a"."name" FROM "user" AS "a"', ())
Requires Python 3.9 or later.
Verify before relying
- Whether the package supports all major SQL dialects or only a subset (Flavor configuration hints dialect support but specifics are unclear).
- Performance characteristics when building very large or deeply nested queries.
- Integration patterns with specific database drivers (psycopg2, mysql-connector, etc.) beyond parameter tuple generation.
Package facts
| License | BSD-3-Clause (permissive) |
| Python support | supports the current Python release (>=3.9) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | none |
| Maintenance | actively maintained — 133 days since the last release |
| First released | |
| Downloads | 106,014/month — #12,672 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: python_sql-1.8.1-py3-none-any.whl
Keywords: SQL, database, query
Tags
More Python Modules packages
Converts domain names between Unicode and…
permissive · top 100 on PyPI
setuptoolsSetuptools is a Python build backend and…
permissive · top 100 on PyPI
PyYAMLPyYAML parses and emits YAML 1.1 data format,…
permissive · top 100 on PyPI
pydanticPydantic validates Python data structures…
permissive · top 100 on PyPI
annotated-typesProvides reusable metadata objects for use with…
permissive · top 100 on PyPI
typing-inspectionProvides runtime tools to inspect and…
permissive · top 100 on PyPI
databricks-sqlProvides a Python interface for querying and…
permissive · top 15,000 on PyPI
sqlparamsConverts SQL queries between different…
permissive · top 5,000 on PyPI
gw-dsl-parserConverts graphic-walker workflow definitions…
permissive · top 15,000 on PyPI
SQLAlchemySQLAlchemy is a Python SQL toolkit and Object…
permissive · top 100 on PyPI
django-query-builderBuilds and executes complex SQL queries…
permissive · top 15,000 on PyPI
jinjasql2Generates parameterized SQL queries from Jinja2…
permissive · top 15,000 on PyPI
sql-metadataParses SQL queries using sqlglot and extracts…
permissive · top 5,000 on PyPI
ipython-sqlAdds SQL magic commands to IPython and Jupyter…
permissive · top 15,000 on PyPI
sqladminGenerates a web-based admin interface for…
permissive · top 5,000 on PyPI
jinjasqlGenerates parameterized SQL queries from Jinja2…
permissive · top 15,000 on PyPI