Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass a string variable into a gql query

How in the world do I pass a string variable into GQL with python?? I can do it fine in SQL but it just isn't working. here is what I have:

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

This has been killing me but should be really simple I feel.

Thanks!

like image 295
clifgray Avatar asked Jun 24 '12 08:06

clifgray


2 Answers

This should work:

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

GqlQuery syntax examples:

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

like image 181
Ashwini Chaudhary Avatar answered Sep 18 '22 01:09

Ashwini Chaudhary


parameters can be bound by position or name, look at the GqlQuery class documentation for more info.

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)
like image 24
Harald Brinkhof Avatar answered Sep 20 '22 01:09

Harald Brinkhof