liveBook · Manning

Python DB-API

As explained in DuckDB in Action

Python DB-API 2.0 is a standard specification for Python interfaces to relational database systems.

Python DB-API 2.0 standardizes a practical interface for executing SQL and retrieving result rows. A connection executes statements immediately and returns an object supporting methods such as fetchone() and fetchall(). section 6.1.2

Use the interface to:

  • Execute SQL through a connection with execute(). section 6.1.2
  • Retrieve one row with fetchone() or all rows with fetchall(). section 6.2.3
  • Pass named parameters instead of concatenating user input, reducing SQL-injection risk and supporting reusable prepared statements. section 6.2.3

Parameterized count query

con.execute("SELECT count(*) FROM over_10m WHERE \"GDP ($ per capita)\" > $gdp", {"gdp": 10000}).fetchone() returns (20,), counting matching countries without embedding the value in SQL text. section 6.2.3

How DuckDB in Action covers it

How deep it goes
Explains connections, execution, fetching, named parameters, and injection mitigation, while deferring broader relational-API details.
Examples
Demonstrates SELECT 42, fetchone(), and a parameterized country-count query over a relational object.
What it assumes
Assumes basic Python and SQL, while demonstrating connection execution and row-fetching operations.
What you can do after
Apply it via DuckDB’s execute() interface to run SQL, bind named values, and fetch results.
How it connects
Connects SQL, prepared statements, parameterized queries, relational APIs, and SQL injection.

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.