Skip to content

t-strings for parameterized quries #594

Description

@doerwalter

Are there plans to add support for using t-strings (introduced with PEP 750 in Python 3.14) for parameterized queries?

Currently doing a parameterized queries looks somewhat like this:

per_id = 42
cursor.execute("select * from person where per_id=:per_id", per_id=per_id)

The code contains the part per_id four times. Using t-strings can reduce this to two:

per_id = 42
cursor.execute(t"select * from person where per_id={per_id}")

psycopg supports t-string too: https://www.psycopg.org/psycopg3/docs/basic/tstrings.html

A function that executes Oracle queries specified as t-strings might look like this:

def execute(cursor, template):
	query = []
	args = {}
	for part in template:
		if isinstance(part, str):
			query.append(part)
		else:
			argname = f"p{len(args)+1}"
			query.append(f":{argname}")
			args[argname] = part.value
	return cursor.exexute("".join(query), args)

and can be called like this:

execute((cursor, t"select * from person where per_id={per_id}")

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions