liveBook · Manning

Cursor

As explained in Practices of the Python Pro

A database cursor is an object that executes database statements and provides access to their results.

A database cursor executes SQL statements and provides access to their results. Query cursors let you iterate through returned rows; non-query statements produce no results. section 6.3.1

A reusable execution method can create a cursor, execute a statement, and return the cursor. Wrapping these steps in a transaction helps preserve the database’s last working state when execution fails. section 6.3.1

def _execute(self, statement, values=None):           #1
        with self.connection:
            cursor = self.connection.cursor()
            cursor.execute(statement, values or [])   #2
            return cursor

The reusable SQLite helper accepts SQL and optional values, executes it through a cursor inside a transaction, and returns the cursor. section 6.3.1

How Practices of the Python Pro covers it

How deep it goes
Defines cursor behavior, demonstrates execution and transactions, and stops short of explaining cursor internals.
Examples
Uses a DatabaseManager._execute method to run parameterized SQL through a cursor.
What it assumes
Assumes a Python SQLite connection and SQL statements while teaching cursor creation and execution from the persistence layer.
What you can do after
Build reusable SQLite statement execution that returns query results and supports parameter values.
How it connects
Ties cursors to SQL queries, transactions, SQLite connections, and CRUD operations.

Also explained in

Compare how 7 books explain cursor →

Related concepts

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