Search code examples
google-app-enginegqlquery

GAE gqlquery with variables


If i want to run a GqlQuery with a variable i've set is that possible?

for example:

myNumber = 4

myResult = db.GqlQuery("SELECT * from myData WHERE filter = myNumber")

this results in:

Parse Error: Invalid WHERE condition at symbol

Am i going about this all wrong? Thanks for your time.


Solution

  • According to The Google Appengine Documentation, your code might look like this:

    myNumber = 4
    myResult = db.GqlQuery("SELECT * FROM myData WHERE filter = :num", num=myNumber)
    

    or

    myNumber = 4
    myResult = db.GqlQuery("SELECT * FROM myData WHERE filter = :1", myNumber)
    

    depending on whether you want to use a named or positional variable.