Android SQLiteDatabase query with LIKE

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

  }

You should give the filter with wildcards ;)

"A%"

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.


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

Tags:

Android