Android ormlite like() function is not working

user977625 picture user977625 · Oct 4, 2011 · Viewed 8.5k times · Source

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.

Answer

Justin Pollard picture Justin Pollard · Jun 26, 2013

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