首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >GAE中的游标和with_cursor()

GAE中的游标和with_cursor()
EN

Stack Overflow用户
提问于 2012-03-02 01:47:53
回答 3查看 543关注 0票数 0

我使用cursor()和with_cursor()逻辑从gae模型中获取记录,就像分页中使用的那样。但是我不确定如何检查db中是否有任何其他记录是由游标指向的。我在一些iterations.when中获取这些记录的块,我在第一次迭代中得到了我需要的结果,然后在下一次迭代中,我想检查模型中没有任何记录,但我在这个stage.please上没有得到任何空/无游标值,这让我知道如何使用python在谷歌应用程序引擎中执行光标检查。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-03-02 12:25:59

如果您的模型正在扩展ndb,则应该使用fetch_page()。如果它没有扩展ndb,您可能需要考虑更改。它不是完全向后兼容的,但数据不需要迁移。

代码语言:javascript
运行
复制
from google.appengine.ext.ndb import model

class User(model.Model):
   name = model.StringProperty()

users, cursor, more = User.query(User.name == "Jon").fetch_page(25)

if more: 
    # Do next page logic
    next_users = User.query(User.name == "Jon").fetch_page(25, start_cursor=cursor)
票数 1
EN

Stack Overflow用户

发布于 2012-03-02 02:06:06

我对此不是百分之百确定,但我过去所做的是将最后一个光标与实际光标进行比较,我想我注意到它们是相同的,所以我得出结论,它是最后一个光标。

票数 0
EN

Stack Overflow用户

发布于 2012-03-02 10:01:23

如果计算查询结果的数量,并且该数量等于fetch,则需要获取更多的结果。如果结果数少于您已获取的所有查询结果。

为了演示这个想法:我使用这个函数在App Engine和Google Apps电子表格之间传输数据。传输是以页面为单位进行的;此函数尝试优化页面大小。参数“cursor”保持光标不动。第一次参数‘’cursor‘= None。args' page‘保存页面大小。查询可以是这样的: query = aLotofData.all()

代码语言:javascript
运行
复制
def makePage(args, proces, query):

    if args['Cursor'] != 'NONE' : query.with_cursor(args['Cursor'])  # next page with cursor

    page = []                                                   # transfer the selected entities in pages
    lines = 0                                                   # lines per page counter
    fetch = int(args['Page'])                                   # fetch can vary; because we optimize the page transfer 
    while lines < 0.8 * int(args['Page']) :                     # optimize pagination when page less than 80% filled
        count = 0                                               # number of entities processed per fetch
        for entity in query.fetch(fetch) :
            count += 1
            page_line = proces(entity)
            if page_line :           
                page.append(page_line)                          # variable function call to preces the entity
                lines += 1
        if count < fetch :                                      # no more entities ?
            cursor = 'NONE'                                     # last page, no cursor 
            break
        cursor = query.cursor()                                 # save current cursor fot he next run                
        query.with_cursor(cursor)                               # set the cursor for the fetch iteration (optimize)
        fetch = int(args['Page']) - lines                       # and fetch the number of page lines left to fill

    logging.debug('Transfered : %d next_cursor : %s' %(lines, cursor)) 

    return simplejson.dumps({'cursor' : cursor, 'page' : page})

函数"proces“处理每个实体。当proces返回None时,该行不会附加到当前页。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9521289

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档