user3168467
user3168467

Reputation: 19

How to search a column in sqlite by a string

I would like to return a true / false value when the string match with the column of sqlite. How can I implement the search function?

How to use cursor to do searching?

public boolean searchLuggage(String code) {

 ArrayList<String> luggageCheck = new ArrayList<String>();
 SQLiteDatabase db = this.getReadableDatabase();

 try {
     Cursor cursor = db.query(true, TABLE_LUGGAGES, new String[] { your field list }, SearchColumnname + "='" + code + "'", null, null, null, null, null);

     if (cursor != null) {
         cursor.moveToFirst();
         for (int i = 0; i < cursor.getCount(); i++) {
            luggageCheck.add(cursor.getString(0));
            cursor.moveToNext();
         }
         cursor.close();
         return true;
     }
     return false;
} catch (Exception e) {
    e.printStackTrace();
}
   return true;
}




// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_LUGGAGE_TABLE = "CREATE TABLE " + TABLE_LUGGAGES + "(" + KEY_NAME + " TEXT NOT NULL," + KEY_CODE + " TEXT NOT NULL," + KEY_NUMBER + " TEXT NOT NULL );";
    db.execSQL(CREATE_LUGGAGE_TABLE);
}

Upvotes: 1

Views: 500

Answers (1)

Greg Ennis
Greg Ennis

Reputation: 15381

Replace this

 Cursor cursor = db.query(true, TABLE_LUGGAGES, new String[] { your field list }, SearchColumnname + "='" + code + "'", null, null, null, null, null);

with this

 Cursor cursor = db.query(true, TABLE_LUGGAGES, null, SearchColumnname + "= ?", new String[] { code }, null, null, null, null);

Upvotes: 1

Related Questions