我到底如何使用 python 将字符串变量传递给 GQL?我可以在 SQL 中做的很好,但它只是不工作。这是我所拥有的:
personalposts = db.GqlQuery("select * from PersonalPost where user_id = %s order by created desc limit 30" % user_id)
这一直在杀死我,但我觉得应该很简单。
谢谢!
我到底如何使用 python 将字符串变量传递给 GQL?我可以在 SQL 中做的很好,但它只是不工作。这是我所拥有的:
personalposts = db.GqlQuery("select * from PersonalPost where user_id = %s order by created desc limit 30" % user_id)
这一直在杀死我,但我觉得应该很简单。
谢谢!
这应该有效:
personalposts = db.GqlQuery("select * from PersonalPost where user_id =:1 order by created desc limit 30",user_id)
GqlQuery
语法示例:
q = GqlQuery("SELECT * FROM Song WHERE composer = 'Lennon, John'")
q = GqlQuery("SELECT __key__ FROM Song WHERE composer = :1", "Lennon, John")
q = GqlQuery("SELECT * FROM Song WHERE composer = :composer", composer="Lennon, John")
来源: https ://developers.google.com/appengine/docs/python/datastore/gqlqueryclass
参数可以通过位置或名称绑定,请查看GqlQuery类文档以获取更多信息。
所以你可以做
personalposts = db.GqlQuery("select * from PersonalPost where user_id = :1 order by created desc limit 30", user_id)
或者
personalposts = db.GqlQuery("select * from PersonalPost where user_id = :id order by created desc limit 30", id = user_id)
创建一个这样的查询:
query = PersonalPost.all()
query.filter('user_id', user_id)
query.order('-created')
或使用:在 '%s' 周围加上单引号 !!
personalposts = db.GqlQuery("select * from PersonalPost where user_id = '%s' order by created desc" % user_id)