尝试显示错误的DATABASE.update

时间:2012-09-21 19:45:14

标签: android database

应用简介:添加联系人/编辑联系人

- Contato.java //显示联系人列表视图,当itemClicked显示信息对话框(名称/电话)和3按钮(确定/更改/删除)时,Alter按钮将用户发送给:

- Adicionarcontato.java有要编辑的信息,但是当我编辑并点击按钮“Salvar”(保存)错误时:The application Mensagem(process com.example.mensagem) has stopped unexpectedly. Please try again.

这是ListView的Contato.java的代码。

 private void ListaContatos(){      
    ListView user = (ListView) findViewById(R.id.lvShowContatos);
    //String = simple value ||| String[] = multiple values/columns
    String[] campos = new String[] {"nome", "telefone"};

    list = new ArrayList<String>();
    c = db.query( "contatos", campos, null, null, null, null, null);
    c.moveToFirst();
    if(c.getCount() > 0) {
        while(true) {
           list.add(c.getString(c.getColumnIndex("nome")).toString());
           if(!c.moveToNext()) break;
        }
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, list);

    user.setAdapter(adapter);

    user.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            reg = position;
            c.moveToPosition(reg);
            String nome = c.getString(c.getColumnIndex("nome"));
            String telefone = c.getString(c.getColumnIndex("telefone"));
            ShowMessage(nome, telefone);

        }
    });

}

以下是Adicionarcontato.java中的代码:

 public SQLiteDatabase db;
private String mIndex = "";
private String nomeant,foneant;
static final String userTable = "contatos";

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.adicionarcontato);
    if(getIntent().getExtras() != null) {
        if(getIntent().getExtras().containsKey("reg")) mIndex = getIntent().getExtras().getString("reg");
    }
    db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);
    if(!mIndex.equals("")) {
        Cursor c = db.query(false, "contatos", (new String[] {"nome", "telefone"}), null, null, null, null, null, null);
        c.moveToPosition(Integer.parseInt(mIndex));
        nomeant = c.getString(0);
        foneant = c.getString(1);
        EditText nome1 = (EditText) findViewById(R.id.etNome);
        EditText telefone1 = (EditText) findViewById(R.id.etTelefone);
        nome1.setText(nomeant);
        telefone1.setText(foneant);
    }

    AdicionarContato();
    ResetarInfo();      
}

单击时按钮“Salvar”的代码:

  public void AdicionarContato() {
    // TODO Auto-generated method stub

       final EditText nm = (EditText) findViewById(R.id.etNome);
       final EditText tlf = (EditText) findViewById(R.id.etTelefone);


    Button add = (Button) findViewById(R.id.bSalvarContato);
    add.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            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 salvo com sucesso");
                }
            }
        }
    });
}

LogCat错误显示: Failure 1 (table contatos already exists) on 0x2205b0 when preparing 'create table contatos(nome varchar(50),telefone varchar(20))'.

我的POV:LogCat中的结果表示该表已经存在但是,在代码中我无法看到我在哪里显示我试图创建它,错误,我尝试连接到它而不是创建它。

2 个答案:

答案 0 :(得分:1)

您不需要whereArgs,因为您在where子句中附加了参数。只需提供null代替whereArgs -

db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"'", null);

但使用参数总是更好。它可以防止sql注入,也可以处理转义的特殊字符。在你的情况下 -

db.update("contatos", dataToInsert, "nome=? and telefone=?", whereArgs);

另外,你的whereArgs是错误的。它应该是 -

String[] whereArgs = new String[] {nomeant, foneant};
来自用户Mukesh Soni

FONT

答案 1 :(得分:0)

同样使用whereArgs允许一些SQLite优化, 您的代码中的另一个问题是您不检查更新和插入操作的返回值

db.update("contatos", dataToInsert, "nome='"+nomeant+"' and telefone='"+foneant+"'", null);  

db.insert("contatos", null, valor); 

如果您在操作中取得成功,则应检查返回的更新值 并插入以检查操作是否确实成功。

您还应该在Android上查看关于SQLite的this好教程作为一个很好的起点。