--- id: jinjasql2 version: "0.1.13" license: MIT license_treatment: permissive maintenance: active --- # jinjasql2 — Generate SQL Queries and Corresponding Bind Parameters using a Jinja2 Template License: permissive · Maintenance: active · Downloads: 142.3K/mo ## 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 above — 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 pip install jinjasql2 uv add jinjasql2 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_current - Install friction: low - Maintenance: active - Downloads: 142.3K/month (top 15,000 on PyPI) - Known vulnerabilities: none known ## Tags jinja sql template generation, parameterized sql queries, sql injection prevention, dynamic sql with jinja2, sql bind parameters, template-based sql, jinja sql builder, sql-templating, injection-prevention [View on SkillFed](https://skillfed.io/packages/jinjasql2) · [View on PyPI](https://pypi.org/project/jinjasql2/)