Drivers and binding
Overview
A database driver handles connection, statement execution, parameter binding and result decoding. Keep connection lifecycle in a data-access layer; bind untrusted values instead of concatenating SQL. A pool leases existing connections; close or return them even after errors.
First example
SELECT id,name FROM ai_account WHERE name='O''Brien';Observed SQLite result (rows in JSON):
[ [ 1, "O'Brien" ]]How it works
Python sqlite3 uses ? placeholders with a separate tuple; node-postgres uses $1, $2 and a values array. Placeholders bind values, not table names or sort directions; choose identifiers from an allowlist. Prepared statements and result caching are different. The SQL literals below isolate the expected rows; host-lab.py actually binds values. Explicit ORDER BY makes results deterministic.
Practical example
SELECT id FROM ai_account WHERE name='missing';Observed SQLite result (rows in JSON):
[]Practice and mistakes
Practice: look up a name containing an apostrophe and a missing name. Hint: use a single-element tuple (name,). Check one row for O’Brien and no rows for an unknown name. Mistake: quoting a placeholder or treating it as an identifier.
PostgreSQL 17 example — NOT RUN
PREPARE ai_lookup(text) AS SELECT $1 AS name;EXECUTE ai_lookup('O''Brien');DEALLOCATE ai_lookup;Expected result, not executed: "One row O'Brien; server PREPARE is not the driver API."
Python and JavaScript binding
Python (executed in host lab, db is open):
db.execute('SELECT id,name FROM ai_account WHERE name=?', ("O'Brien",)).fetchall()JavaScript node-postgres (illustrative, NOT RUN; client already connected):
const result = await client.query('SELECT id, name FROM ai_account WHERE name = $1', ["O'Brien"]);