0

The following code is throwing exception because string field has the SQL reserved word "by" in it. How would I escape this reserved word. The code is meant for google datastore.

String field="Hosted by me"
PersistenceManager pm=PMF.get().getPersistenceManager();
    try{
    Query query=pm.newQuery("select from "+SomeObject.class.getName()
            +" where mField=='"+field+"'");
    _logger.info(query.toString());
    SomeObject=query.execute();
    }finally{
        pm.close();
    }

Here is the exception: : org.datanucleus.exceptions.NucleusUserException: Query contains a JDOQL keyword ("by") that is out of order. Keywords can only be used in a defined order.

Win Myo Htet
  • 5,377
  • 3
  • 38
  • 56

1 Answers1

2

Try this:

PersistenceManager pm = ...;
try {
  Query quer = pm.newQuery("select from " + SomeObject.class.getName()
                          " where mField == mFieldParam" +
                          " parameters String mFieldParam");
  List<SomeObject> results = (List<SomeObject>) query.execute("Hosted by me");
} finally {
  ...
}
Dilum Ranatunga
  • 13,254
  • 3
  • 41
  • 52
  • Dilum Thanks! I won't be able to figure it out even if I read the javadoc http://db.apache.org/jdo/api20/apidocs/javax/jdo/PersistenceManager.html#newQuery(java.lang.String) . I would appreciate if you can give me pointers on where I can go read a bit more of this usage explained in human terms. – Win Myo Htet Oct 16 '11 at 00:01
  • Yes, the apache docs are quite poor. The javadocs are atrocious. Look at Google's own docs with GAE. Start wih this: http://code.google.com/appengine/docs/java/datastore/jdo/queries.html – Dilum Ranatunga Oct 16 '11 at 07:15