Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将字符串变量传递到gql查询中_Python_Google App Engine_Google Cloud Datastore_Gql - Fatal编程技术网

Python 将字符串变量传递到gql查询中

Python 将字符串变量传递到gql查询中,python,google-app-engine,google-cloud-datastore,gql,Python,Google App Engine,Google Cloud Datastore,Gql,我该如何使用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

我该如何使用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")

来源:参数可以由位置或名称绑定,有关详细信息,请参阅类文档

所以你可以

personalposts = db.GqlQuery("select * from PersonalPost where user_id = :1 order by created desc limit 30", 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) 

啊,太好了!所以:1符号只是说这是GQL之后列出的第一个参数?这两个参数中有哪一个在技术上更好?我可以让两个人都工作。一个比另一个快吗?我现在已经试过了,实际上你现在需要跳过单引号,把%s放在双引号结束后的变量上(这里也没有逗号)。
personalposts = db.GqlQuery("select * from PersonalPost where user_id = '%s' order by created desc" % user_id)