SQLAlchemy并处理大量结果

编程入门 行业动态 更新时间:2024-10-24 16:25:35
本文介绍了SQLAlchemy并处理大量结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要从一个大表的所有行中读取数据,但是我不想一次将所有数据都拉到内存中.是否有一个可以处理分页的SQLAlchemy函数?也就是说,将几行存储到内存中,然后在需要时获取更多行.

I need to read data from all of the rows of a large table, but I don't want to pull all of the data into memory at one time. Is there a SQLAlchemy function that will handle paging? That is, pull several rows into memory and then fetch more when necessary.

我了解您可以使用limit和offset作为本文建议,但如果不需要的话,我宁愿不处理.

I understand you can do this with limit and offset as this article suggests, but I'd rather not handle that if I don't have to.

推荐答案

如果您使用的是Flask-SqlAlchemy,请参见query的api/#flask_sqlalchemy.BaseQuery.paginate"rel =" noreferrer> paginate 方法. paginate提供了几种简化分页的方法.

If you are using Flask-SqlAlchemy, see the paginate method of query. paginate offers several method to simplify pagination.

record_query = Record.query.paginate(page, per_page, False) total = record_query.total record_items = record_query.items

首页应为1,否则.total返回异常除以零

First page should be 1 otherwise the .total returns exception divided by zero

更多推荐

SQLAlchemy并处理大量结果

本文发布于:2023-11-30 05:27:14,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648875.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:SQLAlchemy

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!