Search code examples
pythongoogle-app-enginegoogle-cloud-datastoregqlgqlquery

Querying the Google App Engine Datastrore


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)

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)