gpt4 book ai didi

java - Android SQLite,从特定行更新特定字段

转载 作者:行者123 更新时间:2023-12-02 03:55:07 25 4
gpt4 key购买 nike

我正在尝试更新 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 子句中使用电子邮件,即从 Collection 夹更新记录,将 isFavorite 设置为电子邮件所在的(给定值)(以值传递)。

我的查询有问题,被logcat捕获,如下所示

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

任何人都可以帮助我确定我的代码出了什么问题而产生了此错误吗?

P.S 我的 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;
}

关于java - Android SQLite,从特定行更新特定字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35567730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com