SQLAlchemy和Flask

假设 page_index=1,page_size=10;所有分页查询不可以再跟first(),all()等

1.用offset()设置索引偏移量,limit()限制取出

1#filter语句后面可以跟order_by语句 2db.session.query(User.name).filter(User.email.like('%'+email+'%')). 3limit(page_size).offset((page_index-1)*page_size) 4 5

2.用slice(偏移量,取出量)函数

1#filter语句后面可以跟order_by语句 2db.session.query(User.name).filter(User.email.like('%'+email+'%')).slice((page_index - 1) * 3page_size, page_index * page_size)

注释:此方法和第一种相同的效果。

因为:由一下内部方法可知,slice()函数第一个属性就是offset()函数值,第二个属性就是limit()函数值

1@_generative(_no_statement_condition) 2 def slice(self, start, stop): 3 """apply LIMIT/OFFSET to the ``Query`` based on a " 4 "range and return the newly resulting ``Query``.""" 5 6 if start is not None and stop is not None: 7 self._offset = (self._offset or 0) + start 8 self._limit = stop - start 9 elif start is None and stop is not None: 10 self._limit = stop 11 elif start is not None and stop is None: 12 self._offset = (self._offset or 0) + start 13 14 if self._offset == 0: 15 self._offset = None 16 17 @_generative(_no_statement_condition) 18 def limit(self, limit): 19 """Apply a ``LIMIT`` to the query and return the newly resulting 20 21 ``Query``. 22 23 """ 24 self._limit = limit 25 26 @_generative(_no_statement_condition) 27 def offset(self, offset): 28 """Apply an ``OFFSET`` to the query and return the newly resulting 29 ``Query``. 30 31 """ 32 self._offset = offset

3.用paginate(偏移量,取出量)函数,用于BaseQuery

1user_obj=User.query.filter(User.email.like('%'+email+'%')).paginate(int(page_index), 2int(page_size),False) 3#遍历时要加上items 4object_list =user_obj.items

4.filter中使用limit

1#此处不能再跟order_by语句,否则报错 2db.session.query(User.name).filter(User.email.like('%'+email+'%') and limit (page_index - 1) * 3page_size, page_size)
点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

皕杰报表之UUID

​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''

SQLAlchemy和Flask - HelloWorld