0

I am trying to update an SQLiteDatabase but for some reason update always fails and returns 0 (rows updated).

If I remove String selection = null;, then all rows are updated, but I just want to update the row WHERE eId = ? and I provide the eId. All of the values in ContentValues are correct and non null. Been racking my brain for hours now..maybe a fresh set of eyes on this will help, thanks in advance!

NOTE: propertyItem is OK and all of the values coming from my form are correct. The problem breaks at update when I use the selection variable.

/// COLUMN_NAME_EID == "eId"
public int updatePropertyItem (PropertyItem propertyItem) {
    SQLiteDatabase db = mDbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    if ( propertyItem.title != null )values.put(DatabaseContract.DataEntry.COLUMN_NAME_TITLE, propertyItem.title);
    if ( propertyItem.category != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_CATEGORY, propertyItem.category);
    if ( propertyItem.serial != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_SERIAL, propertyItem.serial);
    if ( propertyItem.note != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_NOTE, propertyItem.note);
    if ( propertyItem.purchaseDate != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_PURCHASE_DATE, propertyItem.purchaseDate);
    if ( propertyItem.currentValue != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_CURRENT_VALUE, propertyItem.currentValue);
    if ( propertyItem.replacementValue != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_REPLACE_VALUE, propertyItem.replacementValue);
    if ( propertyItem.store != null ) values.put(DatabaseContract.DataEntry.COLUMN_NAME_STORE, propertyItem.store);
    values.put(DatabaseContract.DataEntry.COLUMN_NAME_WARRANTY, propertyItem.warranty);
    values.put(DatabaseContract.DataEntry.COLUMN_NAME_FAVORITE, propertyItem.favorite);

    String selection = DatabaseContract.DataEntry.COLUMN_NAME_EID + " = ? ";
    String[] selectionArgs = new String[] { String.valueOf(propertyItem.eId) };

    int rowsUpdated = db.update(DatabaseContract.DataEntry.TABLE_NAME, values, selection, selectionArgs);

    db.close();
    return rowsUpdated;
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
  • Have you tried using update as `int rowsUpdated = db.update(DatabaseContract.DataEntry.TABLE_NAME, values, DatabaseContract.DataEntry.COLUMN_NAME_EID + " = " + propertyItem.eId, null)` ? – ρяσѕρєя K Jun 15 '17 at 08:45
  • Are you sure that there is a register stored in database with provided eId? – AlexTa Jun 15 '17 at 08:49
  • may i know that the data type of DatabaseContract.DataEntry.COLUMN_NAME_EID column? – Aniruddh Parihar Jun 15 '17 at 08:52
  • you are passing String for this Column value, so you have to put you string with single quote like: 'propertyItem.eId'. – Aniruddh Parihar Jun 15 '17 at 09:21
  • I have a list of property items in a ListView, so I know that these items exist in a database. propertyItem.eId is not null and I have tried with single quote. COLUMN_NAME_EID is a String – WrightsCS Jun 15 '17 at 17:15

1 Answers1

0

You could try what was suggested here

By setting selection = DatabaseContract.DataEntry.COLUMN_NAME_EID + "=" + propertyItem.eId; and passing null as the final argument for update

And then if in the future you need to extend the where clause just use StringBuilder

jpalvarezl
  • 325
  • 2
  • 8