safety

A query built by concatenating user input is a hole waiting for a payload No amount of escaping by hand makes it reliable

bound parameters

const row = db.prepare("SELECT host FROM targets WHERE port = ?").get(port);

The ? is a placeholder The driver sends the value separately from the statement The database never treats that value as SQL

what not to do

// broken
const sql = `SELECT host FROM targets WHERE port = ${port}`;

Concatenation puts input inside the statement Even a value that looks harmless can change the grammar

identifiers

Placeholders cover values, not table or column names For dynamic identifiers use a strict allowlist of known names Do not accept a raw identifier from a request

least privilege

The application account should not own the schema Read and write on the tables it needs No blanket admin for convenience

Next

Copyable patterns in 10 recipes

Posted