[Objective] to achieve fuzzy search of multiple fields in the table.
[method] ORM Lite like
Method: like(columnName,pattern) uses the% wildcard to match, specifies the row data, and returns the matching result Usage demonstration: mDao.queryBuilder().where().like("LastName", "A%").query(); match the LastName at the beginning of A mDao.queryBuilder().where().like("LastName", "s").query(); matches the LastName at the end of S mDao.queryBuilder().where().like("LastName", "art%").query(); match the LastName with art in the middle Corresponding SQL: select * from ` t'person ` where ` LastName ` like 'a%'
[reference article] https://blog.csdn.net/industriously/article/details/50790624
[Code] query the data related to "sun".
Dao<Cashbook,Integer> cashDao=getCash(); //.or().like("month","%"+keys+"%").or().like("money","%"+keys+"%").or().like("payer","%"+keys+"%").or().like("way","%"+keys+"%") List<Cashbook> mdatas= cashDao.queryBuilder().where().like("client", "%"+keys+"%").or().like("month","'%"+keys+"%'").or().like("money","'%"+keys+"%'").or().like("payer","'%"+keys+"%'").or().like("way","%"+keys+"%").query(); for(Cashbook cashbook:mdatas){ String isIncome; if(cashbook.getIncome()==0){ isIncome="expenditure"; }else{ isIncome="income"; } codeList.add(new TableCashData(cashbook.getPayer(),cashbook.getPayee(),cashbook.getClient(),cashbook.getMonth(),cashbook.getMoney(),cashbook.getWay(),cashbook.getRemark(),isIncome,cashbook.getDate(),false)); } initColumn();
[error reporting] "no such column: Sun", recognized the keyword as "column name", I don't know why! (it's not like that)
10-07 16:28:37.812 27355-27355/menu.bottombar.bottombar E/SQLiteLog: (1) no such column: Grandchildren 10-07 16:28:37.826 27355-27355/menu.bottombar.bottombar W/System.err: java.sql.SQLException: Problems executing Android query: SELECT * FROM `tb_crashbook` WHERE ((((`client` LIKE '%Grandchildren%' OR `month` LIKE '%Grandchildren%' ) OR `money` LIKE '%Grandchildren%' ) OR `payer` LIKE ''%Grandchildren%'' ) OR `way` LIKE '%Grandchildren%' ) 10-07 16:28:37.827 27355-27355/menu.bottombar.bottombar W/System.err: at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22) at com.j256.ormlite.android.AndroidCompiledStatement.getCursor(AndroidCompiledStatement.java:184) at com.j256.ormlite.android.AndroidCompiledStatement.runQuery(AndroidCompiledStatement.java:65) at com.j256.ormlite.stmt.SelectIterator.<init>(SelectIterator.java:55) at com.j256.ormlite.stmt.StatementExecutor.buildIterator(StatementExecutor.java:247) at com.j256.ormlite.stmt.StatementExecutor.query(StatementExecutor.java:196) at com.j256.ormlite.dao.BaseDaoImpl.query(BaseDaoImpl.java:265) at com.j256.ormlite.stmt.QueryBuilder.query(QueryBuilder.java:361) at com.j256.ormlite.stmt.Where.query(Where.java:503) at menu.bottombar.bottombar.Activity.CashbookActivity$OrmLiteTest.searchKeys(CashbookActivity.java:318) at menu.bottombar.bottombar.Activity.CashbookActivity.searchKeywords(CashbookActivity.java:145) at menu.bottombar.bottombar.Activity.CashbookActivity.access$300(CashbookActivity.java:39) at menu.bottombar.bottombar.Activity.CashbookActivity$3$1.onClick(CashbookActivity.java:122) at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:176) at android.os.Handler.dispatchMessage(Handler.java:102)
[solution]
Observe the Error statement carefully: select * from 'tb'crash' where ((((` client ` like '% sun%' or 'month ` like'% sun% ') or' money ` like '% sun%') or 'payer ` like'% sun% ') or' way ` like '% sun%'), there are two "'" symbols, you can manually remove the first and last of the statement, and the reason is not clear.
List<Cashbook> mdatas= cashDao.queryBuilder().where().like("client", "%"+keys+"%").or().like("month","'%"+keys+"%'").or().like("money","'%"+keys+"%'").or().like("payer","%"+keys+"%").query();
Please give me some advice!