Skip to content Skip to sidebar Skip to footer

Appengine Datastore Query Escaping Single Quote (')

I have used javax.jdo.Query like here JDO for Google App Engine: escaping quotes. Yet, my query string with single quote (') keep getting exploded. Query query = pm.newQuery('sele

Solution 1:

Building a query by string concatenation is almost always a risky thing to do, even when SQL Injection attacks aren't possible. (They aren't with GAE.)

See http://code.google.com/appengine/docs/java/datastore/jdo/queries.html#Introducing_Queries and note the bit on "parameter substitution".

Solution 2:

The example code in the document only cover a single parameter substitution. Here is a bit more.

Query query = pm.newQuery(Book.class);
query.setFilter("mArtist == artist && mTitle == title");
query.declareParameters("String artist,String title");              
List<Book> list = (List<Book>) query.execute("Famous Writer","We Won't Give Up");

Some SO questions worth reading :

How to dynamically build JDO Queries on multiple parameters

Google Datastore problem with query on *User* type

Post a Comment for "Appengine Datastore Query Escaping Single Quote (')"