Nidhi Gupta
Nidhi Gupta

Reputation: 19

How to use cursors in Google App Engine for fetching records from a db database in batches?

I am trying to fetch data from a db database in batches and copy it to an ndb database, using a cursor. My code is doing it successfully for the first batch, but not fetching any further records. I did not find much information on cursors, please help me here.

Here is my code snippet: 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")

Basically, how do I set the next cursor to iterate over?

Upvotes: 0

Views: 103

Answers (1)

Nidhi Gupta
Nidhi Gupta

Reputation: 19

def post(self):

    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)

Upvotes: 1

Related Questions