数据库更新应用程序崩溃 [英] Database update crashing app
问题描述
code:
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");
}
}
所以,MINDEX是在previous acitvity联系人(这是我选择和点击的项目/接触,然后通过索引到新的活动),这样的指数,如果EditTexts已经BLANK它将增加一个新的联系人,如果EditTexts有一个值,并得到改变会改变被点击联系人值(名称/电话)。但是,当我打的按钮来保存它崩溃我的应用程序,但错误是在 db.update
行。
db.update(contatos,dataToInsert,诺姆='+ nomeant +'和telefone ='+ foneant +',whereArgs);
等等因此,我猜whereClause或whereArgs是错误的,但我没有在Android中的编程智能化程度高。
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.
推荐答案
您不需要whereArgs在这里,因为你是在where子句本身附加的参数。就在地方whereArgs的NULL来 -
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 -
db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"'", null);
但它始终是最好使用的参数。它prevents SQL注入和也需要转义特殊字符的照顾。在你的情况 -
But it is always better to use the arguments. It prevents sql injection and also takes care of escaping special characters. In your case -
db.update("contatos", dataToInsert, "nome=? and telefone=?", whereArgs);
另外,你的whereArgs是错误的。它应该是 -
Also, your whereArgs is wrong. It should be -
String[] whereArgs = new String[] {nomeant, foneant};
这篇关于数据库更新应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!