Question

I am trying to learn how to work with Google App Engine, I'm looking at their datastore example for querying here I have never done SQL, GQL or the like. So can someone please break down this string and explain what each part is doing?

# GqlQuery interface constructs a query using a GQL query string
q = db.GqlQuery("SELECT * FROM Person " +
                "WHERE last_name = :1 AND height <= :2 " +
                "ORDER BY height DESC",
                "Smith", max_height)
Was it helpful?

Solution

The result of the query is assigned to "q"

db.GqlQuery is the gqlquery method in the db class

"SELECT * FROM Person " + means in english select everything from model Person

"WHERE last_name = :1 AND height <= :2 " + last_name is a field and :1 is the first variable (same with height and :2 - second variable) AND joins the requests

"ORDER BY height DESC", order by field height descending

"Smith", max_height) variable 1 and 2

q = db.GqlQuery("SELECT * FROM Person " +
     "WHERE last_name = :1 AND height <= :2 " +
     "ORDER BY height DESC",
     "Smith", max_height)

OTHER TIPS

As someone commmented earlier, if you aren't coming from a SQL background (and even then I think it's a mistake) you should consider not using GQL. There is no real benefit using GQL over the model and Query objects, and many people come to the appengine table with preconceived ideas based on SQL of how appengine should work, which they have to unlearn.

To perform the same query using the model you would

q = Person.all().filter("last_name = ","Smith").filter("height <= ",max_height).order("-height")

And as the other person mentioned, if you are just starting out you should seriously consider switching to ndb, unless you have a body of pre-existing code you are already working with that is based on db

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top