开发者

Escaping string literal "by", sql reserved word, while creating java Query object

开发者 https://www.devze.com 2023-04-13 01:39 出处:网络
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.

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

精彩评论

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