Code:
final String nome = nm.getText().toString();
final String telefone = tlf.getText().toString();
if(nome.length() != 0 && telefone.length() != 0){
if(mIndex.equals("")) {
ContentValues valor = new ContentValues();
valor.put("nome", nome);
valor.put("telefone", telefone);
db.insert("contatos", null, valor);
ShowMessage("Sucesso","O Contato " + nome + " foi salvo com sucesso");
}
else {
String[] whereArgs = {"nome", "telefone"};
ContentValues dataToInsert = new ContentValues();
dataToInsert.put("nome", nome);
dataToInsert.put("telefone", telefone);
db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"' ", whereArgs);
ShowMessage("Sucesso","O Contato " + nome + " foi editado com sucesso");
}
}
So, mIndex is the index of the Contacts in the previous acitvity(which i selected and clicked the item/contact and then passed the index to the new activity) so, if the EditTexts are already BLANK it will ADD a new contact, if the EditTexts have a value and get changed it will Alter the Clicked Contacts value (name/telephone). But when i hit the button SAVE it crashes my app but the error is in the db.update line.
db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"' ", whereArgs); so therefore i guess the whereClause or the whereArgs is wrong, but as i dont have high intelligence in Android programming.
You don’t need the whereArgs here since you are attaching the arguments in the where clause itself. Just supply null to in place of whereArgs –
But it is always better to use the arguments. It prevents sql injection and also takes care of escaping special characters. In your case –
Also, your whereArgs is wrong. It should be –