Android ormlite like() 函数不起作用
我对此很陌生,请帮助我。
我正在尝试使用 ormlite like(column name,value) 函数,但这对我不起作用。但是当我测试全文时,它的工作方式就像“eq”函数。
我的代码是,
try {
QueryBuilder<MakeDTO, Integer> qb = makeDao.queryBuilder();
qb.where().like("madeCompany", filterKey);
PreparedQuery<MakeDTO> pq = qb.prepare();
return makeDao.query(pq);
} catch (SQLException e) {
throw new AppException(e);
}
谢谢。
I am new to this, please help me.
I am trying to use ormlite like(column name,value) function, but this is not working for me. But when I test full text it is working like "eq" function.
My Code is,
try {
QueryBuilder<MakeDTO, Integer> qb = makeDao.queryBuilder();
qb.where().like("madeCompany", filterKey);
PreparedQuery<MakeDTO> pq = qb.prepare();
return makeDao.query(pq);
} catch (SQLException e) {
throw new AppException(e);
}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一个老问题,但我刚刚解决了(ORMLite 的文档不太清楚)。您需要将查询参数包装在“%”中,以便告诉 ORMLite 查询字符串的哪一侧可以匹配任意数量的字符。
例如,如果您希望查询与包含您的字符串的任何 madeCompany 匹配,请使用以下命令:
An old question, but something I just solved (ORMLite's documentation isn't that clear). you need to wrap your query parameter in "%"'s in order to tell ORMLite which side of your query string can match any number of characters.
For example, if you want your query to match any madeCompany that contains your string use the following:
非常简单,您要求它完全是字符串“madeCompany”,如果您想进行部分匹配,则需要使用 % 通配符等。
Pretty simple, you're asking it to be exactly the string 'madeCompany', if you want to do partial matching you need to use the % wildcard etc.
上述答案可以解决like查询问题,但存在SQL注入风险。如果'filterKey'的值为',则会引发SQLException,因为SQL将是SELECT * FROM XXX WHERE
xxx
LIKE '%'%'。您可以使用 SelectArg 来避免,例如这种情况:参考: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_3.html#index-select-arguments
The answers above can resolved the like query problem, but has SQL injection risk. If the value of 'filterKey' is ', it will cause SQLException, because the SQL will be SELECT * FROM XXX WHERE
xxx
LIKE '%'%'. You could use SelectArg to avoid, example for this case:Reference: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_3.html#index-select-arguments