I've been diving into the new t-string feature from PEP 750, which is meant for SQL sanitization in Python, and I came across a video comparing t-strings to the traditional method of using placeholders for safe SQL queries. The gist of it is that t-strings can make complex SQL queries much easier to read and manage, but for simpler queries with just one or two parameters, the standard method is still the most straightforward. I'm curious about what others think regarding the use of t-strings for handling complex SQL statements in Python. Do you see practical benefits or potential drawbacks?
5 Answers
I totally agree! If you wrap your query using t-strings and create a parameterized version, you get the best of both worlds: the readability from f-strings and the security of parameterization.
If you’re working with PostgreSQL, I’d recommend using `psycopg`'s `sql` module. It’s been around for ages and handles these kinds of situations really well without needing to roll your own solution.
Sure, t-strings have potential, but the risk of introducing security holes is real, especially if someone accidentally switches from a t-string to a regular string. I feel like they might complicate things unnecessarily for common cases.
I'm not convinced about t-strings for SQL. They may make the code prettier, but they don't add security. It's more like a syntax sugar than a protective shield—so use them wisely!
T-strings look promising for this sort of task! However, I’d suggest waiting for a SQL library to officially support them. If you want to implement something in the meantime, I’d recommend creating a wrapper for your SQL engine rather than using `sanitize_sql` as shown in the video. This way, you can separate the query and parameters effectively.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically