我有一个简单的问题:
在GQL syntax summary中
<condition> := <property> {< | <= | > | >= | = | != } <value>但在example here中
if users.get_current_user():
user_pets = db.GqlQuery("SELECT * FROM Pet WHERE owner = :1",
users.get_current_user())什么是:1?
根据语法,那里应该有:=。
谢谢。
发布于 2010-11-01 13:59:12
您发布的语法链接上的GQL语法如下:
SELECT [* | __key__] FROM <kind>
[WHERE <condition> [AND <condition> ...]]
[ORDER BY <property> [ASC | DESC] [, <property> [ASC | DESC] ...]]
[LIMIT [<offset>,]<count>]
[OFFSET <offset>]
<condition> := <property> {< | <= | > | >= | = | != } <value>
<condition> := <property> IN <list>
<condition> := ANCESTOR IS <entity or key>最后三行中的:=表示主表达式中的<condition>可以替换为这三个<condition>语句中:=右侧的表达式。
在这个示例中,他们选择了*而不是__key__,<kind>是Pet,只有一个WHERE条件,并且没有ORDER BY、LIMIT或OFFSET子句。
因此,主表达式简化为:
SELECT * FROM Pet WHERE <condition>然后我们可以替换第一个条件表达式,即:
<condition> := <property> {< | <= | > | >= | = | != } <value>结果是:
SELECT * FROM Pet WHERE <property> {< | <= | > | >= | = | != } <value>在本例中,<property>为owner,运算符为=,<value>为:1,即:
SELECT * FROM Pet WHERE owner = :1根据the documentation for the GqlQuery class的说法,:1表示该值将绑定到GqlQuery()的第一个参数(在查询之后)。因此,在本例中,该值为users.get_current_user()。
更新:
我认为这两种方法都不起作用,因为它们都缺少一个双引号来结束查询字符串。但是,以下两种方法都应该有效:
query = db.GqlQuery(
"SELECT * FROM Rep WHERE author = :1",
users.get_current_user())
user = users.get_current_user()
query = db.GqlQuery(
"SELECT * FROM Rep WHERE author = :1",
user)一旦有了query对象,就可以对其调用fetch()以获取列表形式的项。或者,您也可以直接在查询上迭代。
https://stackoverflow.com/questions/4065942
复制相似问题