Home » Android » Android SQLiteDatabase query with LIKE

Android SQLiteDatabase query with LIKE

Posted by: admin April 23, 2020 Leave a comment

Questions:

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

public Cursor fetchNamesByConstraint(String filter) {

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

    return mCursor;
}

I call the function with “A” as the filter, but my cursor is returning a 0 count when it should at least return me a 1. Anyone can see what’s wrong with the code?

How to&Answers:

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.

Answer:

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

Answer:

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

  }

Answer:

You should give the filter with wildcards 😉

"A%"

Answer:

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