user5808807
user5808807

Reputation: 71

Android SQLite, update specific field from specific row

I am trying to update a specific column in my record within SQLite - the object has various attributes, but I just want to update a single field within that row. Here are my codes:

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;
}

I am using the email for my where clause, i.e update record from favorites, set isFavorite to (given value) where email is (passed in value).

There is a problem with my query, which was caught by logcat, as shown below

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

Can anyone help me identify what is wrong with my codes to produce this error?

P.S my FavoriteObject class has other attributes other than just email and isFavorite, but in this case I am not trying to update them at all

Upvotes: 0

Views: 2416

Answers (2)

m.qadhavi
m.qadhavi

Reputation: 84

Try to make email in where clause be argument too, i try to change your code but didn't test yet :

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;
}

Upvotes: 2

MikeT
MikeT

Reputation: 57073

Looks like it's complaining about the email address, perhaps the @.

try

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

ie. single quotes around the email address.

Upvotes: 0

Related Questions