【问题标题】:Parameter binding using GQL in Google App Engine在 Google App Engine 中使用 GQL 进行参数绑定
【发布时间】:2010-11-23 05:56:38
【问题描述】:

好的,我有这个模式:

class Posts(db.Model):
  rand1 = db.FloatProperty()
  #other models here

还有这个控制器:

class Random(webapp.RequestHandler):
  def get(self):    
      rand2 = random.random()
      posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")
      #Assigning values for Django templating
      template_values = {
          'posts_query': posts_query,
           #test purposes
          'rand2': rand2,
          }

      path = os.path.join(os.path.dirname(__file__), 'templates/random.html')
      self.response.out.write(template.render(path, template_values))

因此,当添加一个实体时,会生成一个随机浮点数 (0-1),然后当我需要抓取一个随机实体时,我希望能够只使用一个简单的 SELECT 查询。它错误:

BadArgumentError('Missing named arguments for bind, requires argument rand2',)

如果我去的话,现在可以了:

posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > 1 ORDER BY rand LIMIT 1")

很明显我的查询是错误的;如何在 where 语句中使用变量:S

【问题讨论】:

    标签: python django google-app-engine binding gql


    【解决方案1】:

    替补:

     "...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1")
    

    与:

      "...WHERE rand1 > :rand2 ORDER BY rand LIMIT 1", rand2=rand2)
    

    或者

      "...WHERE rand1 > :1 ORDER BY rand LIMIT 1", rand2)
    

    查看更多信息:“The Gql query class

    有趣的是,我大约 2 小时前才知道这一点:P

    【讨论】:

    • 是的,谢谢 - 已经解决了:P 虽然它实际上是:WHERE rand1 > :1 ORDER BY rand1 LIMIT 1", rand2) 我不小心用 'rand' 而不是 'rand1',以防其他人想知道并需要帮助。
    猜你喜欢
    • 2010-11-20
    • 2010-09-08
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2011-07-16
    • 1970-01-01
    相关资源
    最近更新 更多