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
