Android ormlite like() 函数不起作用

发布于 2024-12-08 02:09:21 字数 423 浏览 0 评论 0原文

我对此很陌生,请帮助我。

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

山有枢 2024-12-15 02:09:21

一个老问题,但我刚刚解决了(ORMLite 的文档不太清楚)。您需要将查询参数包装在“%”中,以便告诉 ORMLite 查询字符串的哪一侧可以匹配任意数量的字符。

例如,如果您希望查询与包含您的字符串的任何 madeCompany 匹配,请使用以下命令:

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);
}

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:

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);
}
不如归去 2024-12-15 02:09:21

非常简单,您要求它完全是字符串“madeCompany”,如果您想进行部分匹配,则需要使用 % 通配符等。

public Where<T,ID> like(java.lang.String columnName,
                        java.lang.Object value)
                 throws java.sql.SQLException
Add a LIKE clause so the column must mach the value using '%' patterns.
Throws:
java.sql.SQLException

Where.like(java.lang.String, java.lang.Object)

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.

public Where<T,ID> like(java.lang.String columnName,
                        java.lang.Object value)
                 throws java.sql.SQLException
Add a LIKE clause so the column must mach the value using '%' patterns.
Throws:
java.sql.SQLException

Where.like(java.lang.String, java.lang.Object)

浊酒尽余欢 2024-12-15 02:09:21

上述答案可以解决like查询问题,但存在SQL注入风险。如果'filterKey'的值为',则会引发SQLException,因为SQL将是SELECT * FROM XXX WHERE xxx LIKE '%'%'。您可以使用 SelectArg 来避免,例如这种情况:

try {
    String keyword = "%"+filterKey+"%";
    SelectArg selectArg = new SelectArg();
    QueryBuilder<MakeDTO, Integer> qb = makeDao.queryBuilder();
    qb.where().like("madeCompany", selectArg);
    PreparedQuery<MakeDTO> pq = qb.prepare();
    selectArg.setValue(keyword);
    return makeDao.query(pq);
} catch (SQLException e) {
    throw new AppException(e);
}

参考: 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:

try {
    String keyword = "%"+filterKey+"%";
    SelectArg selectArg = new SelectArg();
    QueryBuilder<MakeDTO, Integer> qb = makeDao.queryBuilder();
    qb.where().like("madeCompany", selectArg);
    PreparedQuery<MakeDTO> pq = qb.prepare();
    selectArg.setValue(keyword);
    return makeDao.query(pq);
} catch (SQLException e) {
    throw new AppException(e);
}

Reference: http://ormlite.com/javadoc/ormlite-core/doc-files/ormlite_3.html#index-select-arguments

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文