A Ba A Ba - 5 months ago 42
Java Question

GQL disallowed literal error, google datastore

I am trying to use GQL to get some data back from the datastore.

When I do a "SELECT * FROM Kind" request, it works and I get data back.

However when I try "SELECT * FROM kind where num < 1234", I get a disallowed literal error.

I even tried to do it with quotations - "SELECT * FROM kind where num < '1234'",
but I get the same error.

Has anyone run into this before?

Here is the code:

Query<Entity> query = Query.gqlQueryBuilder(Query.ResultType.ENTITY,
"SELECT * FROM " + kind + " WHERE num < '100'"
).build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
Entity result = results.next();
myList.add(result.getString("num"));

Answer

You need to bind the query parameter rather than directly adding it into the query.

Query<Entity> query = Query.gqlQueryBuilder(Query.ResultType.ENTITY,             
                          "SELECT * FROM " + kind + " WHERE num < @num")
                      .setBinding("num", 100)
                      .build();
QueryResults<Entity> results = datastore.run(query);
while (results.hasNext()) {
    Entity result = results.next();
    myList.add(result.getString("num"));
...
Comments