cx_Oracle:如何迭代结果集?

发布于 2024-07-03 23:52:31 字数 31 浏览 9 评论 0原文

有多种方法可以迭代结果集。 各自的权衡是什么?

There are several ways to iterate over a result set. What are the tradeoff of each?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暗恋未遂 2024-07-10 23:52:36

还有 psyco-pg 似乎是这样做的......根据我的收集,它似乎创建了类似字典的行代理来将键查找映射到查询返回的内存块中。 在这种情况下,获取整个答案并在行上使用类似的代理工厂似乎是有用的想法。 但仔细想想,它感觉更像是 Lua,而不是 Python。

此外,这应该适用于所有 PEP-249 DBAPI2.0 接口,不仅仅是 Oracle,或者您的意思只是使用 Oracle最快

There's also the way psyco-pg seems to do it... From what I gather, it seems to create dictionary-like row-proxies to map key lookup into the memory block returned by the query. In that case, fetching the whole answer and working with a similar proxy-factory over the rows seems like useful idea. Come to think of it though, it feels more like Lua than Python.

Also, this should be applicable to all PEP-249 DBAPI2.0 interfaces, not just Oracle, or did you mean just fastest using Oracle?

暮光沉寂 2024-07-10 23:52:35

我的首选方法是游标迭代器,但首先设置游标的 arraysize 属性。

curs.execute('select * from people')
curs.arraysize = 256
for row in curs:
    print row

在此示例中,cx_Oracle 将一次从 Oracle 256 行中获取行,从而减少需要执行的网络往返次数

My preferred way is the cursor iterator, but setting first the arraysize property of the cursor.

curs.execute('select * from people')
curs.arraysize = 256
for row in curs:
    print row

In this example, cx_Oracle will fetch rows from Oracle 256 rows at a time, reducing the number of network round trips that need to be performed

眼前雾蒙蒙 2024-07-10 23:52:34

规范的方法是使用内置的游标迭代器。

curs.execute('select * from people')
for row in curs:
    print row

您可以使用 fetchall() 一次获取所有行。

for row in curs.fetchall():
    print row

使用它可以很方便地创建包含返回值的 Python 列表:

curs.execute('select first_name from people')
names = [row[0] for row in curs.fetchall()]

这对于较小的结果集很有用,但如果结果集很大,可能会产生严重的副作用。

  • 必须等待整个结果集返回
    您的客户端进程。

  • 您可能会占用客户端的大量内存来保存
    构建列表。

  • Python 可能需要一段时间来构造和解构
    无论如何你都会立即丢弃的列表。


如果您知道结果集中返回了单行,则可以调用 fetchone() 来获取单行。

curs.execute('select max(x) from t')
maxValue = curs.fetchone()[0]

最后,您可以循环遍历结果集,一次获取一行。 一般来说,与使用迭代器相比,这样做没有特别的优势。

row = curs.fetchone()
while row:
    print row
    row = curs.fetchone()

The canonical way is to use the built-in cursor iterator.

curs.execute('select * from people')
for row in curs:
    print row

You can use fetchall() to get all rows at once.

for row in curs.fetchall():
    print row

It can be convenient to use this to create a Python list containing the values returned:

curs.execute('select first_name from people')
names = [row[0] for row in curs.fetchall()]

This can be useful for smaller result sets, but can have bad side effects if the result set is large.

  • You have to wait for the entire result set to be returned to
    your client process.

  • You may eat up a lot of memory in your client to hold
    the built-up list.

  • It may take a while for Python to construct and deconstruct the
    list which you are going to immediately discard anyways.


If you know there's a single row being returned in the result set you can call fetchone() to get the single row.

curs.execute('select max(x) from t')
maxValue = curs.fetchone()[0]

Finally, you can loop over the result set fetching one row at a time. In general, there's no particular advantage in doing this over using the iterator.

row = curs.fetchone()
while row:
    print row
    row = curs.fetchone()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文