2016-02-23 38 views
0

我想更新SQLite中的記錄中的特定列 - 該對象具有各種屬性,但我只想更新該行內的單個字段。這裏是我的代碼:Android SQLite,從特定行更新特定字段

public boolean updateFavorite(String email, int isFavorite){ 
    SQLiteDatabase db = this.getWritableDatabase(); 

    ContentValues args = new ContentValues(); 
    args.put(EMAIL, email); 
    args.put(IS_FAV, isFavorite); 

    int i = db.update(TABLE_FAVORITES, args, EMAIL + "=" + email, null); 

    return i > 0; 
} 

我使用的電子郵件爲我的where子句,即更新從收藏夾中記錄,設置isFavorite來(給定值),其中電子郵件(以值傳遞)。

有一個與我的查詢,這是由logcat中抓住了,如下圖所示

android.database.sqlite.SQLiteException: near "@sjisis": syntax error (code 1): , while compiling: UPDATE Favorites SET email=?,isFavorite=? WHERE [email protected] 

誰能幫我找出什麼是錯我的代碼,產生這個錯誤的問題嗎?

P.S我FavoriteObject類已經不僅僅是emailisFavorite等其他屬性,但在這種情況下,我並不想更新它們在所有

+1

看起來像是抱怨的電子郵件地址,也許是@。或許嘗試'int i = db.update(TABLE_FAVORITES,args,EMAIL +「='」+ email +「'」,null);'ie。電子郵件地址周圍的單引號.. – MikeT

+0

是的,哈哈哈,這是一個粗心的錯誤。你可以把這個答案,所以我可以接受它!謝謝邁克! – user5808807

回答

0

看起來像是抱怨的電子郵件地址,也許是@。

嘗試

int i = db.update(TABLE_FAVORITES, args, EMAIL + "= ' " + email + "'", null); 

即。電子郵件地址周圍的單引號。

2

試圖在where子句是爭論過電子郵件,我試圖改變你的代碼,但還沒有進行測試:

public boolean updateFavorite(String email, int isFavorite){ 
SQLiteDatabase db = this.getWritableDatabase(); 

ContentValues values= new ContentValues(); 
values.put(EMAIL, email); 
values.put(IS_FAV, isFavorite); 
//add arguments for where clause 
String[] args = new String[]{email}; 

int i = db.update(TABLE_FAVORITES, values, "EMAIL=?", args); 

return i > 0; 
}