Update database, android

I've created a method to update an entry to the database within my android application.

 public int updateEntryById(ContentValues diaryValues) {

    String  updateWhere = Diary.DiaryItem.COLUMN_NAME_ID + " = " + Diary.DiaryItem.COLUMN_NAME_ID;
    Log.i(TAG, "UpdateWhere query values" + updateWhere);
    SQLiteDatabase db = diaryDbHelper.getWritableDatabase();
    ContentValues updateValues = new ContentValues();

    updateValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_TITLE, "");
    updateValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_DATE, "");
    updateValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_TIME, "");
    updateValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_ENTRY, "");
    updateValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_LAT, "");
    updateValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_LONG, "");

    Log.i(TAG, "UPDATE" + updateWhere);
    //performs the update
    int count = db.update(
            Diary.DiaryItem.TABLE_NAME, 
            updateValues, 
            updateWhere, 
            null);

    return count;
}

A problem I am finding is that the Log.i(TAG, "UpdateWhere query values" + updateWhere); Doesn't actually give the ID rather the _ID = _ID, which is the column name. instead I want it to be where _ID = 1 for example.

Any ideas? Thanks in advance

protected void saveDiaryItem() {
        ContentValues saveValues = new ContentValues();

        saveValues.put(Diary.DiaryItem.COLUMN_NAME_ID, mID.getText().toString());
        saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_TITLE, mTitle.getText().toString());
        saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_DATE, mDate.getText().toString());
        saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_TIME, mTime.getText().toString());
        saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_ENTRY, mEntry.getText().toString());
        saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_LAT, mLat.getText().toString());
        saveValues.put(Diary.DiaryItem.COLUMN_NAME_DIARY_LONG, mLong.getText().toString());

        Log.i(TAG, "About to insert data" + saveValues);
        DAO diarySave = new DAO(getBaseContext());

        diarySave.updateEntryById(saveValues);
        Log.i(TAG, "IM BACK!"); 
    }

Answers


Your function name is updateEntryById, but you don't pass any ID. Do you know which id you want to update?

Currently you have this line

String  updateWhere = Diary.DiaryItem.COLUMN_NAME_ID + " = " + Diary.DiaryItem.COLUMN_NAME_ID;

which put Diary.DiaryItem.COLUMN_NAME_ID; on both sides of the '=' wign, so you can't expect to get a proper id value.

Shouldn't you get this id as argument of your function?

public int updateEntryById(ContentValues diaryValues, int id) 

It also seems that you don't use the arg ContentValues diaryValues It it normal?


Need Your Help

Assigning textbox values to session

c# asp.net session textbox auto-update

I cant understand whats wrong with this simple code.