I’m having some difficulty understanding the update command for SQLite.
In my example I am perform an update database, but if there’s an error I’m delivering an error message. If the user trys to update a record that doesn’t exist, I’m not getting an error message.
Here is my code, first the update command, and then my call:
public long updateContact(String firstName, String lastName, String address, String city, String state,
int zipCode, long mobileNumber, long altNumber, String email) {
ContentValues contactInfo = new ContentValues();
contactInfo.put(KEY_FNAME, firstName);
contactInfo.put(KEY_LNAME, lastName);
contactInfo.put(KEY_ADDR, address);
contactInfo.put(KEY_CITY, city);
contactInfo.put(KEY_STATE, state);
contactInfo.put(KEY_ZIP, zipCode);
contactInfo.put(KEY_MOBILE, mobileNumber);
contactInfo.put(KEY_ALT, altNumber);
contactInfo.put(KEY_EMAIL, email);
String where = "FIRSTNAME = '" + firstName + "' AND LASTNAME = '" + lastName + "'";
return db.update(DATABASE_TABLE, contactInfo, where, null);
}
The call:
if (searchTerm.length() > 0) {
id = db.updateContact(fName, lName, address.getText().toString().trim(),
city.getText().toString().trim(), state.getSelectedItem().toString().trim(), Integer.parseInt(zip.getText().toString().trim()),
Long.valueOf(mobile.getText().toString().trim()), Long.valueOf(alternate.getText().toString().trim()),
email.getText().toString().trim());
if (id == -1) {
DisplayErrorDialog("I'm sorry, we can not update the contact with the present data.");
}
}
The update is working when I give it valid data. But when I give it invalid data (name that isn’t in the database), I’m not getting the appropriate error message.
The return value is the number of rows updated, not the id. Check for 0, not -1. This implies you could be updating more than one record, which is how database updates work generally.