GQL disallowed literal error, google datastore
Asked Answered
S

1

6

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"));
Strati answered 15/6, 2016 at 13:57 Comment(0)
T
5

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"));
...
Tenrec answered 15/6, 2016 at 14:29 Comment(8)
I'm glad to hear it! Thanks for using Cloud Datastore (☞゚ヮ゚)☞Tenrec
I am just curious, where did you learn how to do this? I want to be able to do this from memory or at least know where to look for answers, other than stackoverflow and google haha.Strati
I'm the PM for Datastore, so memory :) Initially though, I just read code from GitHub samples. For example: github.com/GoogleCloudPlatform/java-docs-samples/blob/master/…Tenrec
Thanks, I am always looking for more resources to learn from. Thank you for the link, it will be very helpful for my project. Are there any other resources similar to this related to GAE that I could use?Strati
I had another question about the datastore. Is it possible for me get aggregated data back? I am trying to find the average number between two range values (dates).Strati
Consider the same reference file you can see that you can also use literal instead of binding (though you need to explicitly enable it). see: github.com/GoogleCloudPlatform/java-docs-samples/blob/master/…Stanislaus
Currently the Google Cloud Datastore does not support aggregation (other than distinct) or functions over different fields. For complete GQL syntax see this: cloud.google.com/datastore/docs/apis/gql/…Stanislaus
@Stanislaus Thank you Ozarov!Strati

© 2022 - 2024 — McMap. All rights reserved.