Skip to main content

Utils for Jinja & sqlite

Must setup an .env pointing to a database file DB_FILE; otherwise, will default to test.db.

Setup Jinja Environment to fetch via .sql file

Assumes a basic Jinja environment has been setup:

from jinja2 import Environment
assert isinstance(env, Environment)

Sync

Query the environment using sqlite3,viz:

from utils_jinja_sqlite import get_rows, get_row
# base
params = dict(a="'hello world'")
sql_stmt = env.get_template("test.sql").render(params)

# get all
rows = get_rows(sql_stmt=sql_stmt)
type(rows) # generator

# get one
row = get_row(sql_stmt=sql_stmt) # gets only the first row
type(row) # dict

Async

Query the environment using a aiosqlite3,viz:

from utils_jinja_sqlite import get_rows, get_row
import asyncio
# base
params = dict(a="'hello world'")
sql_stmt = env.get_template("test.sql").render(params)

# get all
rows = a_rows(sql_stmt=sql_stmt)
type(rows) # co-routine
rows_result = asyncio.run(rows)
type(rows_result) # generator

# get one
row = a_row(sql_stmt=sql_stmt)
type(row) # co-routine
row_result = asyncio.run(row)
type(row_result) # dict

Note that this will not work:

for first_stmt_row in get_rows(sql_stmt=sql_stmt1):
    for second_stmt_row in get_rows(sql_stmt=sql_stmt2):
        ... # the first sql_stmt has not yet terminated

SQL string literal binder funcs

Instead of quoting a string literal can use a helper function

from utils_jinja_sqlite import quote_sql_string
params = dict(a=quote_sql_string(hello world))

Release files for utils-jinja-sqlite 0.0.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for utils-jinja-sqlite 0.0.6
File Size Uploaded
utils-jinja-sqlite-0.0.6.tar.gz 4.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for utils-jinja-sqlite 0.0.6
File Interpreter ABI Platform
utils_jinja_sqlite-0.0.6-py3-none-any.whl Python 3 none any Details

Total release size: 9.5 kB

Release files / utils-jinja-sqlite-0.0.6.tar.gz

Download URL utils-jinja-sqlite-0.0.6.tar.gz
Size 4.7 kB
Tags Source
SHA-256 checksum
How to use checksums
4439f2ffc9c2ed12e1eb2aa263760a5580e51526b8ee30dfcb62c8ce35f9430c
BLAKE2b-256 checksum
How to use checksums
9bd04143b9d2f03a90c80d9412a5e10a39ab1155ddc4bb367d4b34e779b19328
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0b3 CPython/3.10.5 Darwin/21.5.0

Release files / utils_jinja_sqlite-0.0.6-py3-none-any.whl

Download URL utils_jinja_sqlite-0.0.6-py3-none-any.whl
Size 4.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
35b5ec3c687a199c6ebd00578c84e9da32a88560b828ecd3749af39c1b072770
BLAKE2b-256 checksum
How to use checksums
d273e8831d23d96c91f22d7167a3c6e00094ab772cab4884f06bbe5dd9466266
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.0b3 CPython/3.10.5 Darwin/21.5.0

Release history Release notifications | RSS feed

This release

0.0.6 This release

2 release files

0.0.5

2 release files

0.0.4

2 release 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