๐Ÿ—„๏ธ
๐Ÿ—„๏ธ Data & Storage with the Larz Stack ยท LARZ-701 ยท Session 2 of 4

Injection-Safe Queries with larzsql

SQL injection is one of the oldest and most damaging bugs - and it's still everywhere. larzsql makes it impossible by construction: every value is a bound parameter, never inlined text.

โฑ 8 min read๐Ÿ“… Updated 2026-07-27๐Ÿ“Š Tables, charts & examples

SQL injection - where user input is tricked into becoming executable SQL - remains one of the most common and catastrophic security bugs, decades after it was first understood. The usual advice ('always use parameterised queries') relies on developers remembering, every time. larzsql takes a stronger approach: it builds queries so that user values can't become SQL, making injection impossible by construction.

Why injection happens

Injection happens when a value is glued into the SQL text as code instead of treated as data. larzsql never does that.

Dangerous (string-building)Safe (larzsql)
f"...WHERE email='{input}'"value becomes a bound ? parameter
Value glued into SQL textValue travels in a separate params list
Relies on you remembering to escapeImpossible to inline a value at all
One slip = a breachSafe by construction, every time

The root cause is always the same: mixing untrusted data into the query as if it were trusted code. Remove the ability to do that, and injection can't occur.

Safe by construction

larzsql produces a (sql, params) pair - the SQL has placeholders, the values are passed separately to the driver - so user input is always data, never code.

from larzsql import select
sql, params = (select('users')
    .where(email=user_input)      # bound, never inlined
    .where(active=True)
    .order_by('-created').limit(10).build())
# sql:    SELECT * FROM users WHERE email = ? AND active = ? ORDER BY created DESC LIMIT 10
# params: [user_input, True]    # values kept separate from the SQL
๐ŸŽฏ
Safety as a property of the tool, not your memory. Because larzsql only ever emits a placeholder (?) plus a separate params list, there is no code path where a value gets inlined into the SQL string - so injection isn't 'unlikely if you're careful', it's structurally impossible. Identifiers (table/column names) are validated too. You get injection-safety for free, without having to remember to do the right thing on every single query.

Build queries safely

  • Every value is a bound parameter - never inlined into SQL text.
  • Identifiers validated - table/column names checked, not trusted.
  • Rich builder - where/joins/order/limit, ? or %s paramstyles.
  • Works with real databases - the (sql, params) pair feeds any DB-API driver.

๐ŸŽฏ Key takeaways

  • SQL injection - user input becoming executable SQL - is still one of the most damaging bugs.
  • It happens when values are glued into SQL text as code; larzsql never does that.
  • larzsql emits a placeholder + a separate params list, so values are always data, never code - injection is structurally impossible.
  • It validates identifiers and produces a (sql, params) pair for any DB-API driver.

Frequently asked

Doesn't every database library already prevent SQL injection?

The tools exist (parameterised queries), but injection is still rampant because they rely on the developer choosing to use them correctly on every single query - and under deadline pressure, people build SQL strings by hand, especially for dynamic queries, and one slip is a breach. larzsql's difference is that it removes the unsafe path entirely: there's no way to inline a value into the SQL, because the builder only ever produces placeholders plus a separate params list. So safety doesn't depend on discipline or remembering - it's a structural guarantee of the tool. That's a meaningful upgrade over 'we have the safe option available if you remember to use it'.

Can I still write complex queries, or only simple ones?

You can build genuinely useful queries - the builder supports WHERE with a range of operators, multiple conditions, joins, ordering, limits and offsets, and both ? and %s parameter styles for different drivers - all while keeping every value safely bound. For truly exotic, hand-tuned SQL beyond the builder, you can still write raw SQL with explicit parameters (the safe way), so you're never blocked. The point isn't to replace all SQL knowledge; it's to make the common and dynamic cases - exactly where injection bugs creep in - safe by default, while remaining expressive enough for most real application queries.

Session 2 of 4 complete

Next up: Evolving Your Schema with Migrations

Continue to Session 3 โ†’

Related lessons

More in this course

Course home → · Browse all courses →