liveBook · Manning

Parameterized query

As explained in DuckDB in Action

A parameterized query contains placeholders in its statement text, with actual input values supplied separately to the query engine.

A parameterized query keeps statement text separate from input values. Placeholders make dynamic queries reusable and prevent supplied strings from changing SQL semantics, reducing SQL injection risk. section 6.2.3

con.execute("""
SELECT count(*)
FROM over_10m
WHERE "GDP ($ per capita)" > $gdp  #1
""", {
  "gdp":10000  #2
}).fetchone()  #3

Pass the GDP threshold separately through DuckDB's Python DB-API execute function, then retrieve the first result with fetchone. section 6.2.3

Interactive player search

A Streamlit application can pass a player search term as the named $search_term parameter. The query searches winner and loser names, returns distinct matches, and supports calls such as search_players("Novak"). section 9.1.2

How DuckDB in Action covers it

How deep it goes
Explains placeholders, injection risks, parsing benefits, execution, and result retrieval without detailing database internals.
Examples
Demonstrates a GDP threshold count and a Streamlit tennis-player search using named parameters.
What it assumes
Assumes basic SQL and Python familiarity while demonstrating parameter binding and result retrieval through the Python DB-API.
What you can do after
Apply it via DuckDB's execute function to safely run dynamic SQL and fetch results in Python applications.
How it connects
Connects SQL querying with prepared statements, SQL injection, Python DB-API 2.0, DuckDB, and Streamlit.

Also explained in

Compare how 8 books explain parameterized query →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of DuckDB in Action. The section links point to the parts of the book it draws on.