【问题标题】:GQLQuery with_cursor not working带有_cursor的GQLQuery不起作用
【发布时间】:2010-12-02 13:47:59
【问题描述】:

想知道是否有人知道为什么在 GQLQuery 中使用游标似乎无法正常工作。

我正在运行以下内容。

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

感谢您的所有帮助。

【问题讨论】:

  • 如果您的查询将自身限制为 100,为什么 dataLookup.count() 会返回 350?
  • 计数仍然可以返回总查询大小。当您获取时,检索到的结果数组为 100。谢谢!

标签: google-app-engine google-cloud-datastore gql gqlquery


【解决方案1】:

您不应该在查询中使用 LIMIT,因为它只会返回前 100 个结果,并且我假设您想要所有结果,但每次以 100 个为一组进行处理。

这是我要做的(基于您的示例代码):

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) 

【讨论】:

  • 谢谢,就是这样!对代码稍作修改,调用fetch时返回一个数组,因此需要从初始GqlQuery中检索游标。再次感谢您的及时答复!
猜你喜欢
  • 1970-01-01
  • 2017-11-07
  • 2021-09-05
  • 2014-12-10
  • 2011-09-30
  • 2013-01-06
  • 2016-03-17
  • 2018-01-09
  • 2015-11-03
相关资源
最近更新 更多