Android SQLiteDatabase query with LIKE

前端 未结 6 1375
北海茫月
北海茫月 2020-12-02 17:09

I have 3 names, Allakhazam, Beatbox and Cunning in my NAMES Table.

public Cursor fetchNamesByConstraint(String filter) {

    mDb.query(true, DATABASE_NAMES_         


        
相关标签:
6条回答
  • 2020-12-02 17:35

    The rest is up to our desing, cursor is commanly same

    public ArrayList<AboneDataList> getAllPasssiveAboneByDate(String mDate) {
        ArrayList<AboneDataList> foodList = new ArrayList<>();
        AboneDataList food;
        SQLiteDatabase database = dbHelper.getReadableDatabase();
        final String kolonlar[] = {DBHelper.COLUMN_A_ID,
                DBHelper.COLUMN_A_ABONE_NO,
                DBHelper.COLUMN_A_NAME,
                DBHelper.COLUMN_A_IS_STUFF,
                DBHelper.COLUMN_A_COUNTRY,
                DBHelper.COLUMN_A_CITY,
                DBHelper.COLUMN_A_TOWN,
                DBHelper.COLUMN_A_ADDRESS,
                DBHelper.COLUMN_A_PHONE,
                DBHelper.COLUMN_A_MOBILE,
                DBHelper.COLUMN_A_DATE_START,
                DBHelper.COLUMN_A_DATE_END,
                DBHelper.COLUMN_A_COUNT,
                DBHelper.COLUMN_A_IS_ACTIVE,
                DBHelper.COLUMN_A_CARGO,
                DBHelper.COLUMN_A_YURT_ID,
                DBHelper.COLUMN_A_JOURNAL,
                DBHelper.COLUMN_A_MAIL,
                DBHelper.COLUMN_A_BUSINESS,
                DBHelper.COLUMN_A_BIRTH,
                DBHelper.COLUMN_A_GENDER,
                DBHelper.COLUMN_A_ABONE_TYPE,
                DBHelper.COLUMN_A_MEDENI_TYPE};
    
        //String whereClause = DBHelper.COLUMN_A_JOURNAL + " = ? AND " + DBHelper.COLUMN_A_IS_ACTIVE + " = ? ";
        //final String whereArgs[] = {String.valueOf(mJournal),isActive};
    
        //Cursor cursor = database.query(DBHelper.TABLE_ABONE, kolonlar, whereClause, whereArgs,
          //      null, null, DBHelper.COLUMN_A_ID + " ASC");
    
        Cursor cursor = database.rawQuery("select * from " + DBHelper.TABLE_ABONE + " WHERE "
                              + DBHelper.COLUMN_A_DATE_END + " LIKE '%" + mDate + "%'", null);
    
        //Cursor cursor = database.query(DBHelper.TABLE_ABONE, kolonlar, null, null, null, null, DBHelper.COLUMN_A_NAME + " ASC");
        while (cursor.moveToNext()) {
            food = new AboneDataList();
            food.setId(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_ID)));
            food.setAbone_no(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_ABONE_NO)));
            food.setName(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_NAME)));
            food.setIs_stuff(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_IS_STUFF)));
            food.setCountry(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_COUNTRY)));
            food.setCity(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_CITY)));
            food.setTown(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_TOWN)));
            food.setAddress(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_ADDRESS)));
            food.setPhone(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_PHONE)));
            food.setMobile(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MOBILE)));
            food.setDate_start(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_DATE_START)));
            food.setDate_end(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_DATE_END)));
            food.setCount(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_COUNT)));
            food.setIs_active(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_IS_ACTIVE)));
            food.setCargo(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_CARGO)));
            food.setYurt_id(cursor.getInt(cursor.getColumnIndex(DBHelper.COLUMN_A_YURT_ID)));
            food.setJournal(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_JOURNAL)));
            food.setMail(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MAIL)));
            food.setBusiness(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_BUSINESS)));
            food.setBirth(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_BIRTH)));
            food.setGender(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_GENDER)));
            food.setAbone_type(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_ABONE_TYPE)));
            food.setMedeni_hal(cursor.getString(cursor.getColumnIndex(DBHelper.COLUMN_A_MEDENI_TYPE)));
            foodList.add(food);
        }
        database.close();
        cursor.close();
        return foodList;
    }
    
    0 讨论(0)
  • 2020-12-02 17:40
        public java.util.Vector<Products> getsearch(String subcategory,String searchby)
        {
            SQLiteDatabase db=this.getReadableDatabase();
    
            Cursor cursor = db.query(
                TABLE_PRODUCTS, 
                new String[] { SUBCATEGORY, MAIN_CATEGORY, PRODUCT_ID, PRODUCT_NAME, BRAND, PACKAGE_SIZE, PRICE }, 
                SUBCATEGORY + " LIKE '%" + subcategory + "%'",
                null, null, null, null, null);
        }
    
    0 讨论(0)
  • 2020-12-02 17:43

    You can use Raw Queries.

    public Cursor fetchNamesByConstraint(String filter) {
        String query = "SELECT * FROM " + TABLE_NAME + " WHERE " +
                        COLUMN_NAME + " LIKE '%" + filter + "%'" ;
        SQLiteDatabase db = this.getReadableDatabase();
        cursor = db.rawQuery(query, null);
        return mCursor;
    }
    
    0 讨论(0)
  • 2020-12-02 17:51

    this statement will return all the records whose keyname equals string specified by string, if you use wild card, then you can get desired results. Like:

    mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
                KEY_NAME }, KEY_NAME + " LIKE ?",
                new String[] { filter+"%" }, null, null, null,
                null);
    

    Will Lists all the records starting with word in filter.

    mDb.query(true, DATABASE_NAMES_TABLE, new String[] { KEY_ROWID,
                KEY_NAME }, KEY_NAME + " LIKE ?",
                new String[] {"%"+ filter+ "%" }, null, null, null,
                null);
    

    Will Lists all the records containing word in filter.

    0 讨论(0)
  • 2020-12-02 17:52

    Can you try this.....code.......

    public static final String KEY_ROWID="row";
    public static final String KEY_NAME="name";
    
    public Cursor fetchNamesByConstraint(String filter) {
    
      Cursor cursor=mDb.query(true, DATABASE_NAMES_TABLE, null,"row LIKE '%"+filter+"%' or name LIKE '%"+filter+"%'",null, null, null, null);
    
      }
    
    0 讨论(0)
  • 2020-12-02 17:54

    You should give the filter with wildcards ;)

    "A%"
    
    0 讨论(0)
提交回复
热议问题