title: How to use cursors in Google App Engine for fetching records from a db database in batches?
body: <p>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.</p>
  <p>Here is my code snippet:    def post(self):</p>
  <pre><code>    a = 0   
      chunk_size = 2
      next_cursor = self.request.get(&quot;cursor&quot;)
      query = db.GqlQuery(&quot;select * from BooksPost&quot;)
  
      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(&quot;cursor&quot;)
  </code></pre>
  <p>Basically, how do I set the next cursor to iterate over?</p>