Android 数据库更新崩溃应用程序
代码: 因此,mIndex是以前活动中联系人的索引(我选择并单击了项目/联系人,然后将索引传递给了新活动),因此,如果EditText已为空,它将添加一个新联系人,如果EditText有一个值并被更改,它将更改单击的联系人值(姓名/电话)。但是当我点击SAVE按钮时,它会使我的应用程序崩溃,但错误出现在Android 数据库更新崩溃应用程序,android,database,Android,Database,代码: 因此,mIndex是以前活动中联系人的索引(我选择并单击了项目/联系人,然后将索引传递给了新活动),因此,如果EditText已为空,它将添加一个新联系人,如果EditText有一个值并被更改,它将更改单击的联系人值(姓名/电话)。但是当我点击SAVE按钮时,它会使我的应用程序崩溃,但错误出现在db.update行中 db.update(“contatos”,dataToInsert,“nome=”“+nomeant+””和telefone=“+foneant+””,wherergs)
db.update
行中
db.update(“contatos”,dataToInsert,“nome=”“+nomeant+””和telefone=“+foneant+””,wherergs)代码>因此我猜where子句或wherergs是错误的,但我在Android编程中没有高智能。这里不需要wherergs,因为您在where子句本身中附加了参数。仅提供空以代替wherergs-
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");
}
}
但使用参数总是更好的。它防止sql注入,还负责转义特殊字符。就你而言—
db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"'", null);
还有,你的行踪是错的。应该是——
db.update("contatos", dataToInsert, "nome=? and telefone=?", whereArgs);
这里是我的另一个问题的[LINK[(),它有LogCat错误和main.Java的代码,现在我没有打开onCreate中的“VerificaDados();”,所以数据库没有显示错误。试一下Mukesh Soniok建议的答案,它工作了:db.update(“contatos”,dataToInsert,“nome=”+nomeant+“,telefone=”“+foneant+”,null)
谢谢你的帮助。顺便说一句,db.update(“contatos”,dataToInsert,“nome=?and telefone=?”,wherergs);
没有显示错误,但它没有正确保存值。它会的。你的wherergs是错误的。它应该是这样的-String[]wherergs=new String[]{nomeant,foneant};是的,我只是在等待时机。
String[] whereArgs = new String[] {nomeant, foneant};