Android SQLiteDatabase query using LIKE

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 a filter, but my cursor returns 0 counts, when it should at least return me 1. Can anyone see what is wrong with the code?

+28
source share
5 answers

this operator will return all records whose name is equal to the string indicated by the string, if you use a wild card, then you can get the desired results. How:

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

Will display all entries starting with the word in the filter.

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

Will display all entries containing the word in the filter.

+84
source
  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); } 
+4
source

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); } 
+4
source

You must specify a filter using wildcards;)

 "A%" 
+1
source

The rest depends on our design, the cursor is usually the 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
source

All Articles