Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why GreenDAO doesn't support LIKE operator completely?

GreenDAO just supported one position of LIKE operator. It was " LIKE ?"

I wanna fetch records with variety of this operator. such as, " LIKE %?", " LIKE ?%" and " LIKE %?%". but it doesn't supported by GreenDAO.

Also I've used queryRaw() and queryRawCreate(), unfortunately it didn't work, too. For example:

libDocSeriesDao.queryRawCreate( " Where T.Title Like '%?%' Or T.ViewTitle Like '%?%'", aKeyword, aKeyword).listLazy();

Any help would be greatly appreciated.

like image 587
Omid Nazifi Avatar asked Oct 17 '12 05:10

Omid Nazifi


2 Answers

Is more easy:

dao.queryBuilder().where(Properties.SimpleName.like(name+"%")).list()
like image 31
user3111328 Avatar answered Oct 12 '22 20:10

user3111328


The '%' character must not be part of the query string when you use the '?' character. You can use any combination of % when binding the parameter.

Here is an example how to use LIKE queries (taken from greenDAO's unit tests at https://github.com/greenrobot/greenDAO/commit/788313904fa58a0c8628f6b2e016a4a385f344c6):

Query<TestEntity> query = dao.queryBuilder().where(Properties.SimpleString.like("%robot")).build();
TestEntity entity2 = query.uniqueOrThrow();
assertEquals(entity.getId(), entity2.getId());

query.setParameter(0, "green%");
entity2 = query.uniqueOrThrow();
assertEquals(entity.getId(), entity2.getId());

query.setParameter(0, "%enrob%");
entity2 = query.uniqueOrThrow();
assertEquals(entity.getId(), entity2.getId());

The same principle is valid for raw queries. For your example, you should do this:

libDocSeriesDao.queryRawCreate(" Where T.Title Like ? Or T.ViewTitle Like ?", aKeyword, aKeyword).listLazy();

Also, aKeyword must have the % character.

like image 139
Markus Junginger Avatar answered Oct 12 '22 19:10

Markus Junginger