Rohit
Rohit

Reputation: 810

SQLite database update query with multiple where conditions in Android

I want to update my database table with multiple where conditions. I already did with single where condition

db.update(TABLE_MISSING_ITEMS, values, KEY_AUTHOR + " = ?",
                new String[] { String.valueOf(items.getAuthor()) }); 

Now i want 2 where condition.

P.S :- No raw query

Upvotes: 16

Views: 41012

Answers (4)

Samir
Samir

Reputation: 6605

The way I solved my need

   public boolean checkHususiKayit(String baslik, String tarih) {
    boolean varMi = false;
    SQLiteDatabase database = dbHelper.getReadableDatabase();
    final String kolonlar[] = {DBHelper.COLUMN_H_ID,
            DBHelper.COLUMN_H_ID,
            DBHelper.COLUMN_H_BASLIK,
            DBHelper.COLUMN_H_TARIH,
            DBHelper.COLUMN_H_YOK_TUR,
            DBHelper.COLUMN_H_AD,
            DBHelper.COLUMN_H_WEB_ID,
            DBHelper.COLUMN_H_NUMARA,
            DBHelper.COLUMN_H_YURD_ID,
            DBHelper.COLUMN_H_YETKILI_AD,
            DBHelper.COLUMN_H_YETKILI_ID,
            DBHelper.COLUMN_H_TEL,
            DBHelper.COLUMN_H_EMAIL,
            DBHelper.COLUMN_H_ADDRESS,
            DBHelper.COLUMN_H_VAR,
            DBHelper.COLUMN_H_GOREVLI,
            DBHelper.COLUMN_H_YOK,
            DBHelper.COLUMN_H_IZINLI,
            DBHelper.COLUMN_H_HATIMDE};

    String whereClause = DBHelper.COLUMN_H_BASLIK + " = ? AND " + DBHelper.COLUMN_H_TARIH + " = ?"; // HERE ARE OUR CONDITONS STARTS
    String[] whereArgs = {baslik, tarih};


    Cursor cursor = database.query(DBHelper.TABLE_NAME_HUSUSI, kolonlar, whereClause, whereArgs, null, null, null + " ASC");
    while (cursor.moveToNext()) {
        varMi = true;
    }
    database.close();
    cursor.close();
    return varMi;
}

Upvotes: 1

Syed Danish Haider
Syed Danish Haider

Reputation: 1384

Try this simple query db.update(TABLE_FF_CHECKLIST_DATA,contentValues, "FFCHECKLISTID = ? and TASK_ID_CHK = ?" , new String[] {"EHS" , "CTO914"});

where "EHS" is value in column FFCHECKLISTID and CTO914 is value in TASK_ID_CHK.

Upvotes: 0

Deepa MG
Deepa MG

Reputation: 196

THIS CAN ALSO BE DONE LIKE THIS

public void UpdateData(int Cid,int flag,String username,String password)
    {
        SQLiteDatabase database = this.getWritableDatabase();
        ContentValues cv = new ContentValues();

          cv.put("Status",flag);//I am  updating flag here

        database.update(TABLE_NAME, cv, ""+KEY_UserName+"= '"+ username+"' AND "+KEY_CID+"='"+Cid+"'  AND "+KEY_Password+"='"+password+"'" , null);
        database.close();
    }

Upvotes: 0

Manas Bajaj
Manas Bajaj

Reputation: 1133

You can separate the different WHERE conditions with ANDlike this:

db.update(TABLE_NAME,
    contentValues,
    NAME + " = ? AND " + LASTNAME + " = ?",
    new String[]{"Manas", "Bajaj"});

Upvotes: 50

Related Questions