Android 仅当值不为null时才更新sqlite数据库
我有一个更新个人信息的活动 我想知道是否有一种方法只更新非空值,其余的保持不变这里是我的更新函数Android 仅当值不为null时才更新sqlite数据库,android,sqlite,Android,Sqlite,我有一个更新个人信息的活动 我想知道是否有一种方法只更新非空值,其余的保持不变这里是我的更新函数 public boolean modifierMember (String nom ,String prenom ,String tel,String profile ,String etat,int id ){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new Co
public boolean modifierMember (String nom ,String prenom ,String tel,String profile ,String etat,int id ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,nom);
contentValues.put(COL_3,prenom);
contentValues.put(COL_4,tel);
contentValues.put(COL_5,profile);
contentValues.put(COL_k, etat);
db.update(TABLE_Membre,contentValues,"IDM=?",new String[]{Integer.toString(id)});
return true;
}
是的,您可以这样做,一种方法是检查值是否为null,或者检查特定
列的值,在您的情况下,它可以是:
if(nom != null) {
contentValues.put(COL_2,nom);
}
// Same for other fields.
在上面,您可以类似地检查其他值,如果它们不为空,则只添加到contentValues
编辑
它还取决于modifierMember()
函数作为参数传递的内容
例如,如果你通过了“null”,那么你需要像我上面所说的那样进行比较,如果你通过了”
,那么你必须进行相应的比较,这完全取决于你通过了什么作为参数,检查下面针对你的要求更新的方法
public boolean modifierMember (String nom ,String prenom ,String tel,String profile ,String etat,int id ){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
if(nom != null)
contentValues.put(COL_2,nom);
if(prenom != null)
contentValues.put(COL_3,prenom);
if(tel != null)
contentValues.put(COL_4,tel);
if(profile != null)
contentValues.put(COL_5,profile);
if(etat != null)
contentValues.put(COL_k, etat);
db.update(TABLE_Membre,contentValues,"IDM=?",new String[]{Integer.toString(id)});
return true;
}
非常感谢您,非常感谢您编写了所有内容,但是当我这样做时,db.update是否知道在哪里放置每个内容值,而无需我指定每个内容值的去向???我从用户填写的文本字段中获取信息,因此如果他将其留空,则其为空,对吗