gpt4 book ai didi

python - 在 Google App Engine 中使用 GQL 进行参数绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 01:44:06 25 4
gpt4 key购买 nike

好的,我有这个模式:

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

和这个 Controller :

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))

因此,当添加一个实体时,会生成一个随机 float (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

最佳答案

替代:

 "...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

关于python - 在 Google App Engine 中使用 GQL 进行参数绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1338704/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com