Pass string variable to gql request

How in the world to pass a string variable in GQL using python? I can do it well in SQL, but it just doesn't work. here is what i have:

personalposts = db.GqlQuery("select * from PersonalPost where user_id = %s order by created desc limit 30" % user_id) 

It killed me, but it should be very simple, I feel.

Thanks!

+6
source share
3 answers

This should work:

 personalposts = db.GqlQuery("select * from PersonalPost where user_id =:1 order by created desc limit 30",user_id) 
Syntax Examples

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

source: https://developers.google.com/appengine/docs/python/datastore/gqlqueryclass

+8
source
Parameters

can be related by position or name, see the additional GqlQuery documentation.

So you could do

 personalposts = db.GqlQuery("select * from PersonalPost where user_id = :1 order by created desc limit 30", user_id) 

or

 personalposts = db.GqlQuery("select * from PersonalPost where user_id = :id order by created desc limit 30", id = user_id) 
+2
source

Create a query like this:

 query = PersonalPost.all() query.filter('user_id', user_id) query.order('-created') 

or use: With single quotes around '% s' !!

 personalposts = db.GqlQuery("select * from PersonalPost where user_id = '%s' order by created desc" % user_id) 
+1
source

Source: https://habr.com/ru/post/918865/


All Articles