簡體   English   中英

Android SQLite,從特定行更新特定字段

[英]Android SQLite, update specific field from specific row

我正在嘗試更新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=sjkshs@sjisis.com

誰能幫助我確定我的代碼出了什么問題以產生此錯誤?

PS我的FavoriteObject類除了emailisFavorite之外還具有其他屬性,但是在這種情況下,我根本不嘗試更新它們

嘗試使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;
}

好像是在抱怨電子郵件地址,也許是@。

嘗試

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM