Escaping string literal "by", sql reserved word, while creating java Query object
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());
SomeObje开发者_StackOverflow社区ct=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.
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 {
...
}
精彩评论