1

I have an activity which updates person info

i want to know if there is a way to update only the non null values and the rest you leave it the same here is my update function

  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;

}

2 Answers 2

2

yes you can do that, one way to do this would be to check whether the value is null or what for a particular column, in your case it can go like :

if(nom != null) {
    contentValues.put(COL_2,nom);
}
// Same for other fields.

in the above you can check for other values similarly and if they are not null then only add to the contentValues

Edit

It also depends upon what are passing in your modifierMember() function as argument

for e.g if you pass 'null' then you need to compare as i said above, if you pass "" then you have to compare accordingly, this solely depends upon what you pass as the argument

Sign up to request clarification or add additional context in comments.

1 Comment

im gettin the info from a text feild that a user fills so if he leaves it empty so its null right
1

Check below updated method for your requirement,

      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;

      }

2 Comments

thank you so much very nice of you to write all of it
but when i do does the db.update know where to put every content values without me specifying where eacch one goes ???

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.