在 Google App Engine 中使用 GQL 进行参数绑定
好的,我有这个模式:
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
Okay so I have this mode:
class Posts(db.Model):
rand1 = db.FloatProperty()
#other models here
and this 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))
So when an entity is added a random float is generated (0-1) and then when I need to grab a random entity I want to be able to just use a simple SELECT query. It errors with:
BadArgumentError('Missing named arguments for bind, requires argument rand2',)
Now this works if I go:
posts_query = db.GqlQuery("SELECT * FROM Posts WHERE rand1 > 1 ORDER BY rand LIMIT 1")
So clearly my query is wrong; how does one use a variable in a where statement :S
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
:
替换
或
有关详细信息,请参阅:“Gql 查询类< /a>"
有趣的是,我大约 2 小时前才了解到这一点:P
Substitute:
with:
Or
See for more information: "The Gql query class"
The funny thing is that I have just learned this about 2 hrs ago :P