Cursor
A database cursor is an API object or handle that provides access to query results and supports traversing or retrieving them, often incrementally.
A Pythonic Adventure: From Python basics to a working web app
A database cursor is an object that executes database statements and retrieves their results.
- How it is used
- Uses a cursor to execute SQL commands while connecting a Flask application to a SQLite orders database.
- How deep it goes
- Defines the cursor through an analogy and demonstrates creation and execution, while explicitly deferring deeper details.
- Examples
- Demonstrates CREATE TABLE, INSERT orders, and SELECT-based retrieval in an order-tracking application.
- What you can do after
- Operate/configure a cursor to execute table-creation, insertion, and selection commands in a SQLite-backed Flask application.
Read how A Pythonic Adventure: From Python basics to a working web app explains cursor →
Learn SQL in a Month of Lunches
A database cursor is a database programming construct that enables query-result data to be processed one row at a time.
- How it is used
- Processes SELECT results one row at a time when individual evaluation or per-row procedure calls are required.
- How deep it goes
- Explains the DECLARE, OPEN, FETCH, and CLOSE lifecycle through a complete stored-procedure loop and end-of-results handler.
- Examples
- Uses order IDs to total quantities sold at list price and scenarios involving California orders, first orders, and customer messages.
- What you can do after
- Evaluate/understand cursor-based stored procedures and decide when a set-based query can replace row-by-row processing.
Practices of the Python Pro
A database cursor is an object that executes database statements and provides access to their results.
- How it is used
- Executes SQL statements and exposes query results within a reusable persistence-layer helper.
- 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 you can do after
- Build reusable SQLite statement execution that returns query results and supports parameter values.
Coding with AI
A database cursor is an object that executes database statements and accesses their results through an established connection.
- How it is used
- Uses cursors to connect Flask route handlers and model methods to SQLite queries and returned page data.
- How deep it goes
- Explains cursor creation, query use, dependency passing, and connection cleanup, but stops short of database-driver internals.
- Examples
- Demonstrates selecting records, fetching questions, rendering results, generating question sets, and correcting cursor-related import and constructor errors.
- What you can do after
- Operate/configure database-backed Flask code by passing a live cursor into models, fetching records, and closing connections safely.
API Design Patterns
A cursor is a position marker that identifies the point from which the next portion of paged data should be retrieved.
- How it is used
- Guides consumers through collection pages or chunks of a large resource.
- How deep it goes
- Defines termination, opacity, empty-page behavior, and large-resource continuation, without specifying one token representation.
- Examples
- Uses multi-page resource retrieval, empty-result responses, and chunked attachment reads.
- What you can do after
- Operate pagination by passing returned tokens and stopping only when the next token is empty.
MongoDB 8.0 in Action, Third Edition
A cursor is an object representing an incremental result set from a database query and providing methods for traversing its documents.
- How it is used
- Handles large query results through incremental, batched retrieval and supports automatic or manually controlled document traversal.
- How deep it goes
- Explains cursor creation, batching, lazy loading, automatic display, manual iteration, and read timeouts without detailing driver internals.
- Examples
- Uses routes queries, mongosh display behavior, manualIteration(), and a sample_mflix change-stream cursor.
- What you can do after
- Apply it via mongosh to retrieve query results incrementally, choose manual traversal, and monitor change-stream events.
Read how MongoDB 8.0 in Action, Third Edition explains cursor →
Python Concurrency with asyncio
A database cursor is an object that traverses and retrieves rows from a database query result, potentially incrementally.
- How it is used
- Streams large PostgreSQL query results when pagination or full-result loading creates memory, query, or database-load concerns.
- How deep it goes
- Explains pointer-style traversal, asynchronous iteration, prefetching, skipping, and batching, but not cursor internals.
- Examples
- Uses a product query to stream 1,000 rows, skip 500 rows, fetch 100, or print the first five.
- What you can do after
- Operate/configure asyncpg cursors inside transactions to stream, skip, batch, and limit retrieved rows.
Also covered in
- 100 SQL Server Mistakes and How to Avoid Them
- Software Engineering for Data ScientistsIn early access (MEAP)
- HTML5 in Action
- The Quick Python Book, Fourth Edition
- Snowflake Data Engineering
- Write Powerful Rust Macros
- Python How-To
- Acing the CCNA Exam: Volume 1 Fundamentals and Protocols
- Data Analysis with LLMs
What to know first
- SQLSQL is a standardized declarative language for defining, querying, and manipulating data in relational database systems.
- context managerA context manager is a programming construct or object protocol that establishes a managed execution context, performing setup when a block is entered and cleanup when it exits.
- degradationDegradation is a decline in a system’s or component’s performance, quality, or effectiveness relative to an earlier or expected level.
- paginationPagination is the technique of dividing a collection or result set into bounded portions that can be retrieved or displayed separately.
Related concepts
- closehas part · Close is an operation that ends active use of an associated resource or interface and may release or finalize related resources or pending work.
- find()produced by · find() is a MongoDB Query API method that returns a cursor for documents matching an optional query filter in a collection.
- loopuses · A loop is a programming-language control-flow construct that repeatedly executes a block of instructions according to an iteration rule or continuation condition.
- stored procedureused by · A stored procedure is a named executable routine stored in a database and run by its database management system to perform operations.
- query resultuses · A query result is the collection of rows and columns produced by evaluating a database query.
- SQLuses · SQL is a standardized declarative language for defining, querying, and manipulating data in relational database systems.
Questions about cursor
What is cursor?
A database cursor is an API object or handle that provides access to query results and supports traversing or retrieving them, often incrementally.
Which Manning books explain cursor?
A Pythonic Adventure: From Python basics to a working web app, Learn SQL in a Month of Lunches, Practices of the Python Pro, Coding with AI, API Design Patterns, MongoDB 8.0 in Action, Third Edition and Python Concurrency with asyncio.
What should I know before learning cursor?
Manning books that teach cursor usually introduce SQL, context manager, degradation and pagination first.
What are the parts of cursor?
Cursor includes close.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.






