开发者

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 {
  ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜