LogCat errors when Compiling App

2019-06-13 15:18发布

问题:

When i compile the app these errors show:

   sqlite returned: error code = 1, msg = table mensagens already exists
   Failure 1 (table mensagens already exists) on 0x240328 when preparing 'create table mensagens(mensagemsalva varchar(250),mensagemenviada varchar(250))'.
   sqlite returned: error code = 1, msg = table contatos already exists
   Failure 1 (table contatos already exists) on 0x240328 when preparing 'create table contatos(nome varchar(50),telefone varchar(20))'.

My Main.java has this code which is meant to create the database and its tables:

  onCreate(..){
   ...
  db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);
    ...
    VerificaDados();
  }

    private void VerificaDados() {
    // TODO Auto-generated method stub

    try {
        //cria uma TABLE de nome MENSAGENS
        db.execSQL("create table mensagens(mensagemsalva varchar(250),mensagemenviada varchar(250))");
        //ShowMessage("Banco","Criou a tabela de mensagens");
    }
    catch (Exception e) {

        }
    try{
        //cria uma TABLE de nome CONTATOS
        db.execSQL("create table contatos(nome varchar(50),telefone varchar(20))");
        //ShowMessage("Banco","Criou a tabela de contatos");
    }catch (Exception a){

    }

}

idk what it can be because i just run the app and BAM, errors in LogCat.

回答1:

Have a look athe the SQLiteOpenHelper API Docs.

this class takes care of opening the database if it exists, creating it if it does not, and upgrading it as necessary.

You should try to distinguish between onCreate() and onUpdate().



回答2:

Just Comment (//) the VerificaDados(); because the tables are already created.

onCreate(..){
    ...
 db = openOrCreateDatabase("banco.db", Context.MODE_WORLD_WRITEABLE, null);
    ...
   //VerificaDados();
   }