在 SQLite 中导航游标行(例如,我们可以回退/重置游标,即返回第一行吗?)

Navigating cursor rows in SQLite (Can we rewind/reset the cursor i.e. go back to first row for example?)(在 SQLite 中导航游标行(例如,我们可以回退/重置游标,即返回第一行吗?))
本文介绍了在 SQLite 中导航游标行(例如,我们可以回退/重置游标,即返回第一行吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解以下内置函数在顺序处理游标行时如何工作.描述来自 Python 3.1 手册(使用 SQLite3)

I am trying to understand how the following builtin functions work when sequentially processing cursor rows. The descriptions come from the Python 3.1 manual (using SQLite3)

Cursor.fetchone()

获取查询结果集的下一行,返回单个序列.

Fetches the next row of a query result set, returning a single sequence.

Cursor.fetchmany()

获取查询结果的下一组行,返回一个列表.

Fetches the next set of rows of a query result, returning a list.

Cursor.fetchall()

获取查询结果的所有(剩余)行,返回一个列表.

Fetches all (remaining) rows of a query result, returning a list.

因此,如果我有一个循环,在其中我使用 cursor.fetchone() 一次处理一行,而后来的一些代码要求我返回第一行,或者使用 fetchall() 获取所有行,怎么办我做吗?

So if I have a loop in which I am processing one row at a time using cursor.fetchone(), and some later code requires that I return to the first row, or fetch all rows using fetchall(), how do I do it?

这个概念对我来说有点奇怪,尤其是来自 Foxpro 背景,它有一个记录指针的概念,可以移动到光标中的第一行或最后一行(转到顶部/底部),或者转到第n行(前进n)

The concept is a bit strange to me, especially coming from a Foxpro background which has the concept of a record pointer which can be moved to the 1st or last row in a cursor (go top/bottom), or go to the nth row (go n)

任何帮助将不胜感激.

艾伦

推荐答案

Python 3.1 中的 SQLite 接口基于 PEP 249,它只指定游标必须支持对查询结果记录的顺序访问.没有办法回去了.如果您需要返回之前提取的行,则应在第一次提取时将其保存,例如创建一个获取数据的列表(或者实际上,只使用 fetchall).然后,您可以使用该列表并在行之间来回走动.

The SQLite interface in Python 3.1 is based on PEP 249, which only specifies that cursors have to support sequential access to the records of a query result. There's no way to go back. If you need to return to a previously fetched row, you should save it when you first fetch it, e.g. create a list of the fetched data (or actually, just use fetchall). Then you can work with the list and go back and forth between rows as much as you want.

DB API 设计背后的理念是支持代码的高效执行,您只需处理每一行一次.一个典型的循环如下所示:

The idea behind the design of the DB API is to support efficient execution of code where you only need to process each row once. A typical loop looks like this:

for a,b,c in cursor.fetchone():
    # process it

这样,一旦相应的循环迭代完成,游标就可以丢弃对每一行数据的引用.它并没有真正丢失任何东西,因为如果您想保留所有数据,您总是可以从中列出一个列表,但是处理大型数据集的应用程序仍然能够一次处理一个行.当您需要一个一个地处理可迭代的元素时,这有点像使用生成器表达式而不是列表背后的推理.

This way, the cursor can discard the references to each row's data once the corresponding iteration of the loop is finished. It doesn't really lose anything because if you want to keep all the data, you can always make a list out of it, but applications dealing with large data sets still have the ability to process rows one at a time. It's kind of like the reasoning behind using generator expressions rather than lists when you need to process elements of an iterable one by one.

这篇关于在 SQLite 中导航游标行(例如,我们可以回退/重置游标,即返回第一行吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
FastAPI + Tortoise ORM + FastAPI Users (Python) - Relationship - Many To Many(FastAPI+Tortoise ORM+FastAPI用户(Python)-关系-多对多)
Inserting NaN value into MySQL Database(将NaN值插入MySQL数据库)
Window functions not working in pd.read_sql; Its shows error(窗口函数在pd.read_sql中不起作用;它显示错误)
(Closed) Leaflet.js: How I can Do Editing Geometry On Specific Object I Select Only?((已关闭)Leaflet.js:如何仅在我选择的特定对象上编辑几何图形?)
in sqlite update trigger with multiple if/Case Conditions(在具有多个IF/CASE条件的SQLite UPDATE触发器中)