android实现关键字搜索功能

//以关键字查找日记为例,主要是Sql语句

public ArrayList searchNotes(String keywords) {
  if (keywords != null) {
   ArrayList dList = new ArrayList();

//查询日记标题或内容含有关键字的记录
   Cursor cusror = mDb.query(true, DATABASE_TABLE, new String[] { ROWID,
     TITLE, BODY, CREATED, MOOD, WEATHER, COLOR, BACKGROUND, SIZE },
     "TITLE like '%" + keywords + "%'"+" or BODY like '%" + keywords +"%'" ,  null, null, null, null, null);
   while (cusror.moveToNext()) {
    DiaryBean bean = new DiaryBean();
    bean.setTitle(cusror.getString(cusror.getColumnIndex(TITLE)));
    bean.setBody(cusror.getString(cusror.getColumnIndex(BODY)));
    bean.setColor(cusror.getInt(cusror.getColumnIndex(COLOR)));
    bean.setMood(cusror.getInt(cusror.getColumnIndex(MOOD)));
    bean.setId(cusror.getInt(cusror.getColumnIndex(ROWID)));
    bean.setTime(cusror.getString(cusror.getColumnIndex(CREATED)));
    bean.setWeather(cusror.getInt(cusror.getColumnIndex(WEATHER)));
    bean.setSize(cusror.getInt(cusror.getColumnIndex(SIZE)));
    bean.setBackground(cusror.getInt(cusror
      .getColumnIndex(BACKGROUND)));
    dList.add(bean);
   }
   cusror.close();
   return dList;
  } else {
   return null;
  }

你可能感兴趣的:(android,android,关键字搜索,数据库查询)