2017-06-15 50 views
0

我想更新SQLite數據庫但由於某種原因更新總是失敗並返回0(行更新)。Android的SQLite數據庫WHERE語句休息更新

如果我刪除String selection = null;,那麼所有的行都更新,但我只想更新行WHERE eId =?我提供了eId。 ContentValues中的所有值都是正確的且非空。現在已經花費了我的大腦數小時..可能是一個新的眼睛會有所幫助,提前謝謝!

注意:propertyItem是可以的,所有來自我的表單的值都是正確的。當我使用選擇變量時,問題在更新時中斷。

/// 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; 
} 
+0

你有沒有嘗試過使用更新爲'int rowsUpdated = db.update(DatabaseContract.DataEntry .TABLE_NAME,值,DatabaseContract.DataEntry.COLUMN_NAME_EID +「=」+ propertyItem.eId,null)'? –

+0

您確定使用提供的eId在數據庫中存儲了一個寄存器嗎? – AlexTa

+0

我可以知道DatabaseContract.DataEntry.COLUMN_NAME_EID列的數據類型嗎? –

回答

0

你可以嘗試,如果將來你需要擴展where條款只是用什麼建議here

通過設置 selection = DatabaseContract.DataEntry.COLUMN_NAME_EID + "=" + propertyItem.eId;並通過null作爲最後一個參數爲update

然後StringBuilder

+0

我嘗試了通過將selectionArgs作爲null,沒有運氣那裏。 – WrightsCS