Ali Haider
Ali Haider

Reputation: 17

select by specific value in sqlite?

i want to select data on the basis of bloodtype. suppose i want to select all 'A+' bloodtype in my table . what query will be applied.

public void selectbybloodtypes(){
 String s = "A+" ;
 String query = "SELECT * FROM " + TABLE_CONTACTS + "WHERE bloodtypes= '" + s ;

 SQLiteDatabase db = this.getWritableDatabase();
 Cursor cursor = db.rawQuery(query, String[] { });

 Contact contact = null;
 if (cursor.moveToFirst())
 {
     do
     {
         contact = new Contact();
         contact.setbloodtypes(cursor.getString(3));
            Log.d("getAllContacts()", contact.getBloodtypes().toString());
     }while(cursor.moveToNext());
     }
}

Upvotes: 0

Views: 1579

Answers (1)

matiash
matiash

Reputation: 55340

You're missing the end quote for the query parameter, plus a space before the WHERE keyword. Should be:

"SELECT * FROM " + TABLE_CONTACTS + " WHERE bloodtypes = '" + s + "'";

I would also recommend using any of the query() methods instead of rawQuery(). They help avoid common mistakes.

Upvotes: 1

Related Questions