prepared statement
A SQL statement with placeholders, such as ?, whose values are sent separately from the SQL text instead of pasted into it.
In more detail
Because the database never parses the values as SQL, input like a name containing a quote cannot change the statement, which prevents SQL injection. It also lets the database reuse the parsed statement. Always bind values that come from users or other programs.
Examples
db.execute("SELECT body FROM doc WHERE owner = ?", (owner,))INSERT INTO customers (name, city) VALUES (?, ?);Related terms
- SQL injection — An attack where untrusted input is joined into a database query so that part of it runs as SQL, letting an attacker read or change data.
- psycopg — The most widely used Python driver for PostgreSQL databases.
- INSERT — The SQL statement that adds new rows to a table.
- string literal — A fixed text value written directly in a query between single quotes, such as 'Lagos'.
- allowlist — An allowlist names exactly what is permitted and a denylist names what is forbidden, with everything else allowed.
- SQL — The standard language for defining, querying and changing data in a relational database.
- database — A program that stores data in an organised way and answers questions about it, such as PostgreSQL.
Taught in
- Static Application Security Testing with Semgrep — Cloud Security & DevSecOps Engineering
- Injection In All Its Forms — Application Security & Threat Modeling for Engineers
- SQL Injection: The Exploit — Application Security & Threat Modeling for Engineers
- SQL Injection: The Fix — Application Security & Threat Modeling for Engineers
- Inserting Data Safely — PostgreSQL, Indexing & Relational Data Modeling
- Tuples: Fixed Length, Labeled Elements And Variadic Tuples — Production TypeScript