skillfed

jinjasql2

Generate SQL Queries and Corresponding Bind Parameters using a Jinja2 Template

jinjasql2 v0.1.13 142.3K downloads/30d#11,214 on PyPI45
Permissive license MIT Active released

What it is and what it does

JinjaSQL is a template engine that generates parameterized SQL queries from Jinja2 templates. It processes template variables and conditional logic, then outputs a query with placeholders and a separate dictionary or tuple of bind parameters, ensuring values are never inlined into the SQL string itself. The package supports multiple parameter styles (named, format, qmark, numeric, pyformat, asyncpg) to match different database drivers' conventions.

The package is designed for scenarios where raw SQL is necessary—reporting, aggregation, bulk migrations, and multi-table queries—rather than as an ORM replacement. It protects against SQL injection by binding all template variables as parameters, though templates themselves must be trusted code (the sqlsafe and identifier filters allow deliberate inlining when needed). You prepare the query and parameters, then execute them using your database driver of choice.

Use it for:

  • Build dynamic reporting queries with conditional WHERE clauses, GROUP BY, and aggregations that an ORM cannot express cleanly
  • Generate parameterized bulk update or migration scripts using Jinja macros and loops while maintaining SQL injection safety
  • Construct multi-table SELECT queries with optional joins and filters based on runtime conditions
  • Support IN clauses with the inclause filter to bind list elements as separate parameters for database drivers
  • Escape table and column names safely using the identifier filter when dynamic schema references are required

Worth the install?

AI-flagged interpretation of the facts on this page — verify before relying

Generates parameterized SQL queries from Jinja2 templates, automatically binding variables to prevent SQL injection while supporting conditional logic, macros, and multiple parameter styles.

Yes, if you need to generate dynamic SQL queries safely in Python and your use case genuinely requires raw SQL (reporting, complex aggregations, bulk operations). The package is lightweight, actively maintained, has no known vulnerabilities, and supports modern Python versions. Not necessary if an ORM handles your queries adequately.

Install

jinjasql2 on PyPI

pip

pip install jinjasql2

uv

uv add jinjasql2

poetry

poetry add jinjasql2

Installing jinjasql2

Before you install

Low friction: pure Python wheel with only Jinja2 as a runtime dependency. Actively maintained with a recent release and no known vulnerabilities.

License in practice

MIT license permits unrestricted use, modification, and distribution in both open-source and proprietary projects with minimal obligations.

Quickstart

from jinjasql import JinjaSql

j = JinjaSql()
template = "SELECT * FROM users WHERE id = {{ user_id }}"
query, bind_params = j.prepare_query(template, {"user_id": 123})
# query: "SELECT * FROM users WHERE id = :user_id_1"
# bind_params: {"user_id_1": 123}

Verify before relying

  • Performance characteristics when handling large or deeply nested template contexts
  • Compatibility with specific database drivers beyond the examples given (Django, asyncpg)

Package facts

License MIT (permissive)
Python support supports the current Python release (>=3.8)
Install friction low — pure-Python wheel
Runtime dependencies 1 — Jinja2
Maintenance actively maintained — 38 days since the last release
Last repo commit
First released
Downloads 142,297/month — #11,214 on PyPI (30-day window, as of 2026-08-14)
Known vulnerabilities none known (OSV.dev, checked 2026-08-14)

Evidence: jinjasql2-0.1.13-py3-none-any.whl

Keywords: Jinja2, SQL, Python, Template

Development Status :: 4 - BetaEnvironment :: ConsoleIntended Audience :: DevelopersLicense :: OSI Approved :: MIT LicenseOperating System :: OS IndependentProgramming Language :: Python :: 3Programming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.8Programming Language :: Python :: 3.9

Tags

jinja sql template generationparameterized sql queriessql injection preventiondynamic sql with jinja2sql bind parameterstemplate-based sqljinja sql builder
sql-templatinginjection-prevention

More Database packages