简体   繁体   English

数据库游标

[英]Database Cursor

In Python, what is a database "cursor" most like? 在Python中,数据库“游标”最喜欢什么?

  1. A method within a class 类中的方法

  2. A Python dictionary Python字典

  3. A function 功能

  4. A file handle 文件句柄

I have searched on internet but I am not getting proper justification of this question. 我已经在互联网上进行搜索,但没有获得关于该问题的正当理由。

According to the PEP 249 (Python Database API Specification) , it is an object which is expected to have certain attributes and methods defined. 根据PEP 249(Python数据库API规范) ,该对象应具有定义的某些属性和方法。 Optionally, cursors can support an iteration protocol . 可选地, 游标可以支持迭代协议

This is though a spec, and, your database driver of choice may not follow the API specification. 尽管这是一个规范,并且您选择的数据库驱动程序可能不遵循API规范。 But, this is a more or less general answer to the rather general question. 但是,这或多或少是对相当普遍的问题的普遍回答。

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). 这并不意味着它是一个文件句柄,而游标实际上是一个对象Cursor类的实例(取决于所使用的实际db驱动程序)。

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. 从文件句柄读取将返回字符/字节,从游标读取将返回行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM