HackerReposA FIELD GUIDE TO OPEN SOURCE
ProSponsor the shelf♧ 0
← Back to top all-time
pgjones
sql-tstring
Python2025–2025
pgjones / sql-tstring

sql-tstring

SQL-tString allows for f-string like construction of sql queries

2Distinct people
3Mentions
0Explicit recommenders
162GitHub stars
REPOSITORY AT A GLANCE

What to know before exploring

What it is

SQL-tString allows for f-string like construction of sql queries

GitHub

Where it is used

No documented product use has been verified yet.

What people are saying

No recommendation phrase was found in the indexed comments.

No classified warning is attached to this page. Read the discussions before treating silence as approval.

Also in these discussions

No other repository is repeatedly linked in the same discussions.

This week on Hacker News

Nov 7–Nov 14 UTC · Direct repository links and separately labeled related project stories. Indexed through 11/14/2026, 8:59:20 PM UTC.

No direct repository links were found in the indexed HN discussions over the past seven days. Older discussions are available below.

Mentions over time

HN mentions by week3 dated mentions

Observed mentions: 2025-04-10 21:49:06 UTC to 2025-05-16 12:48:22 UTC. Weeks start Monday, UTC.

012025-04-07 00:00:00 UTC to 2025-04-14 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 commentsApr 72025-04-14 00:00:00 UTC to 2025-04-21 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-04-21 00:00:00 UTC to 2025-04-28 00:00:00 UTC (exclusive): 1 mentions; 0 submissions, 1 commentsApr 212025-04-28 00:00:00 UTC to 2025-05-05 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-05-05 00:00:00 UTC to 2025-05-12 00:00:00 UTC (exclusive): 0 mentions; 0 submissions, 0 comments2025-05-12 00:00:00 UTC to 2025-05-19 00:00:00 UTC (exclusive): 1 mentions; 1 submissions, 0 commentsMay 12

Counts come from the repository’s dated source evidence. Index cutoff: 2026-11-02 08:16:59 UTC.

View mention counts and date ranges
UTC ranges include their start and exclude their end. Select a period to filter the source evidence.
PeriodStart (UTC)End (exclusive, UTC)MentionsSubmissionsComments
Apr 72025-04-07 00:00:00 UTC2025-04-14 00:00:00 UTC101
Apr 142025-04-14 00:00:00 UTC2025-04-21 00:00:00 UTC000
Apr 212025-04-21 00:00:00 UTC2025-04-28 00:00:00 UTC101
Apr 282025-04-28 00:00:00 UTC2025-05-05 00:00:00 UTC000
May 52025-05-05 00:00:00 UTC2025-05-12 00:00:00 UTC000
May 122025-05-12 00:00:00 UTC2025-05-19 00:00:00 UTC110

What Hacker News says

1 sources
pgjones·May 16, 2025Submission

Show HN: SQL-tString a t-string SQL builder in Python

SQL-tString is a SQL builder that utilises the recently accepted PEP-750, https://peps.python.org/pep-0750/, t-strings to build SQL queries, for example, from sql_tstring import sql val = 2 query, values = sql(t"SELECT x FROM y WHERE x = {val}") assert query == "SELECT x FROM y WHERE x = ?" assert values == [2] db.execute(query, values) # Most DB engines support this The placeholder ? protects against SQL injection, but cannot be used everywhere. For example, a column name cannot be a placeholder. If you try this SQL-tString will raise an error, col = "x" sql(t"SELECT {col} FROM y") # Raises ValueError To proceed you'll need to declare what the valid values of col can be, from sql_tstring import sql_context with sql_context(columns="x"): query, values = sql(t"SELECT {col} FROM
Read the rest
y") assert query == "SELECT x FROM y" assert values == [] Thus allowing you to protect against SQL injection. As t-strings are format strings you can safely format the literals you'd like to pass as variables, text = "world" query, values = sql(t"SELECT x FROM y WHERE x LIKE '%{text}'") assert query == "SELECT x FROM y WHERE x LIKE ?" assert values == ["%world"] This is especially useful when used with the Absent rewriting value. SQL-tString is a SQL builder and as such you can use special RewritingValues to alter and build the query you want at runtime. This is best shown by considering a query you sometimes want to search by one column a, sometimes by b, and sometimes both, def search( *, a: str | AbsentType = Absent, b: str | AbsentType = Absent ) -> tuple[str, list[str]]: return sql(t"SELECT x FROM y WHERE a = {a} AND b = {b}") assert search() == "SELECT x FROM y", [] assert search(a="hello") == "SELECT x FROM y WHERE a = ?", ["hello"] assert search(b="world") == "SELECT x FROM y WHERE b = ?", ["world"] assert search(a="hello", b="world") == ( "SELECT x FROM y WHERE a = ? AND b = ?", ["hello", "world"] ) Specifically Absent (which is an alias of RewritingValue.ABSENT) will remove the expression it is present in, and if there an no expressions left after the removal it will also remove the clause. The other rewriting values I've included are handle the frustrating case of comparing to NULL, for example the following is valid but won't work as you'd likely expect, optional = None sql(t"SELECT x FROM y WHERE x = {optional}") Instead you can use IsNull to achieve the right result, from sql_tstring import IsNull optional = IsNull query, values = sql(t"SELECT x FROM y WHERE x = {optional}") assert query == "SELECT x FROM y WHERE x IS NULL" assert values == [] There is also a IsNotNull for the negated comparison. The final feature allows for complex query building by nesting a t-string within the existing, inner = t"x = 'a'" query, _ = sql(t"SELECT x FROM y WHERE {inner}") assert query == "SELECT x FROM y WHERE x = 'a'" This library can be used today without Python3.14's t-strings with some limitations, https://github.com/pgjones/sql-tstring?tab=readme-ov-file#pr..., and I've been doing so this year. Thoughts and feedback very welcome.
85 points · Open discussion

MAKE YOURSELF AT HOME

A few shortcuts

/
Search the collection
?
Open this guide
Esc
Close dialogs and navigation

Your saved repositories and interests stay in this browser.