Dhrub kumar
Dhrub kumar

Reputation: 71

Database Cursor

In Python, what is a database "cursor" most like?

  1. A method within a class

  2. A Python dictionary

  3. A function

  4. A file handle

I have searched on internet but I am not getting proper justification of this question.

Upvotes: 1

Views: 3264

Answers (2)

mhawke
mhawke

Reputation: 87064

Probably it is most like a file handle.

That does not mean that it is a file handle, and a cursor is actually an object - an instance of a Cursor class (depending on the actual db driver in use).

The reason that it's similar to a file handle is that you can consume data from it, but (in general) you can't go back to previously consumed data. Consumption of data is therefore unidirectional. Reading from a file handle returns characters/bytes, reading from a cursor returns rows.

Upvotes: 2

alecxe
alecxe

Reputation: 473833

According to the PEP 249 (Python Database API Specification), it is an object which is expected to have certain attributes and methods defined. Optionally, cursors can support an iteration protocol.

This is though a spec, and, your database driver of choice may not follow the API specification. But, this is a more or less general answer to the rather general question.

Upvotes: 2

Related Questions