• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Updating Database - retaining data issue

Lylio

Lurker
Hi everyone,

I have a very simple app built in Android Studio. I'm trying to update information in a database, but some of the fields are being overwritten as blank. I've created a short video which shows the issue better than I can explain it here (best to watch it on YouTube rather than the preview here):

Basically, I'm using text fields to update parts of the information already stored in the database. The problems is, if I click on my 'update' button, and any of those fields are blank, the data is being erased. Hopefully the video above is a lot more clear.

If it helps, the code for the activity you see in the video is here: https://github.com/Lylio/GradeRunne...lechristine/graderunner/MainMenuActivity.java

and the database helper for the example in the video is here: https://github.com/Lylio/GradeRunne...lylechristine/graderunner/DatabaseHelper.java

I'd be really grateful if anyone could help. I presume the solution would involve saving the data that's already in the database into temporary variables, and then re-writing them when the 'update' button is pressed. But I'm a little unsure of where/how to code that.

Many thanks everyone,

Lylio
 
Well a simple solution would be to do some validation on your Text fields. If they are blank, then tell the user to enter a value, and don't call the database update statements.
 
Thanks for the reply LV426. That was a great suggestion about validating text fields. I tried to implement your idea by checking if the field was not empty, and if it wasn't, then that data should be updated into the database; if the field was empty, then it should be ignored:
updateMethod.jpg

9


But the problem is, the database helper expects 4 String variables to be passed into the update method, so the whole thing didn't work:
updateHelper.jpg

9


I think the only option is to save what's already in the database row into temporary variables, then rewrite them as part of the update process. Do you know how I can save a database field with a specific row ID. Like, say I want to save the String that's in the 3rd row, 2nd column.... I'm struggling to understand how to use a Cursor to implement that.
 
You could write the updateData() method like this:

Code:
public boolean updateData(String id, String course, String assignment, String dueDate) {
  ContentValues contentValues = new ContentValues();
  if (course != null && course.length() > 0) {
    contentValues.put(COL2, course);
  }
  if (assignment != null && assignment.length() > 0) {
    contentValues.put(COL3, assignment);
  }
  if (dueDate != null && dueDate.length() > 0) {
    contentValues.put(COL4, dueDate);
  }
  ...
}

This way you're guaranteed not to overwrite existing fields with an empty string.
 
Oh my God, it works, it works! Thank you so much LV426, I'm so grateful for your help - if you have a Patreon or PayPal I'd be happy to make a donation, I'm so relieved you've solved this for me! Thank you!
 
Hehe! Thanks, it's nice to be appreciated. All advice here provided for free. Your thanks is payment enough.
 
Back
Top Bottom