如何使用Google App Engine中的游标从批处理数据库中批量提取记录?

问题描述

我试图批量从数据库数据库中获取数据,然后使用游标将其复制到ndb数据库中。我的代码在第一个批处理中成功完成了该操作,但未获取任何其他记录。我没有找到有关游标的太多信息,请在这里帮助我。

这是我的代码段:def post(self):

    a = 0   
    chunk_size = 2
    next_cursor = self.request.get("cursor")
    query = db.GqlQuery("select * from BooksPost")

    while a == 0:
        if next_cursor:
            query.with_cursor(start_cursor = next_cursor)
        else:
            a = 1
       
        results = query.fetch(chunk_size)

        for result in results: 
            nbook1 = result.bookname
            nauthor1 = result.authorname
            nbook1 = nBooksPost(nbookname = nbook1,nauthorname = nauthor1)
            nbook1.put()
            next_cursor = self.request.get("cursor")

基本上,如何设置下一个光标进行迭代?

解决方法

def帖子(自己):

    chunk_size = 10
    has_more_results = True
    query = db.GqlQuery("select * from Post")  
    cursor = self.request.get('cursor',None)
    #cursor = query.cursor()
    if cursor:
        query.with_cursor(cursor)
    
    while has_more_results == True:
        
        results = query.fetch(chunk_size)
        new_cursor = query.cursor()
        print("count: %d,results %d" % (query.count(),len(results)))
                
        if query.count(1) == 1:
            has_more_results = True
        else:
            has_more_results = False    
                  
        for result in results: 
            #do this
            
        query.with_cursor(new_cursor)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...