Reputation: 71
In Python, what is a database "cursor" most like?
A method within a class
A Python dictionary
A function
A file handle
I have searched on internet but I am not getting proper justification of this question.
Upvotes: 1
Views: 3264
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
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