0

wondering if anyone knows why using cursors with GQLQuery doesn't seem to be working properly.

I'm running the following.

query = "SELECT * FROM myTable WHERE accountId = 'agdwMnBtZXNochALEglTTkFjY291bnQYpQEM' and lastUpdated > DATETIME('0001-01-01 00:00:00') ORDER BY lastUpdated ASC LIMIT 100"

if lastCursor:    
    dataLookup = GqlQuery(query).with_cursor(lastCursor)
else
    dataLookup = GqlQuery(query)

//dataLookup.count() here returns some value like 350

for dataItem in dataLookup:    
  ... do processing ...

myCursor = dataLookup.cursor()

dataLookup2 = GqlQuery(query).with_cursor(myCursor)

//dataLookup2.count() now returns 0, even though previously it indicates many more batches can be returned

Thanks for all your help.

Nick Johnson
  • 100,655
  • 16
  • 128
  • 198
savagepanda
  • 857
  • 12
  • 25

1 Answers1

2

You should not use a LIMIT in your query, as that will only return the first 100 results, and I assume you want all of them, but process them in batches of 100 each time.

Here's what I would do (based on your example code):

query = GqlQuery("SELECT * FROM myTable WHERE accountId = 
  'agdwMnBtZXNochALEglTTkFjY291bnQYpQEM' and 
  lastUpdated > DATETIME('0001-01-01 00:00:00') ORDER BY lastUpdated ASC")

dataLookup = query.fetch(100) # get first 100 results

for dataItem in dataLookup:
  # do processing

myCursor = query.cursor() # returns last entry of previous fetch

if myCursor:
  # get next 100 results
  dataLookup2 = query.with_cursor(myCursor).fetch(100) 
Tom van Enckevort
  • 4,170
  • 1
  • 25
  • 40
  • Thanks, that was it! slight mod to the code, when fetch is called, it returns an array, thus the cursor needs to be retrieved from the initial GqlQuery. Thanks again for the prompt answer! – savagepanda Dec 02 '10 at 15:09