Problem with Sqlite Android Data Base

Asked

Viewed 174 times

1

So my problem is this one, I’ve tried and researched everything but I couldn’t implement any solution. I tried to update the database with the update and also insert new columns with ALTER TABLE, nothing worked. I am a beginner so please be very specific with the answers. Thank you.

Nome has no column named bairro (code 1): ,while compiling:
INSERT INTO Nome(email,bairro,rua,nomeDoCliente,telefoneResidencial,celular) VALUES (?,?,?,?,?,?)

//Create da classe Helper
db.execSQL("CREATE TABLE "
            + ClientesDAO.TABLE_NAME
            + " (" + ClientesDAO.ID
            + " INTEGER PRIMARY KEY,"
            + ClientesDAO.NOME_CLIENTE + " TEXT,"
            + ClientesDAO.RUA + " TEXT,"
            + ClientesDAO.EMAIL + " TEXT,"
            + ClientesDAO.BAIRRO + " TEXT,"
            + ClientesDAO.TELEFONE + " TEXT,"
            + ClientesDAO.CELULAR + " TEXT);"
);


//insert da classe ClientesDAO
public void insert(Clientes clientes){
    ContentValues cv = new ContentValues();
    cv.put(NOME_CLIENTE, clientes.getNome());
    cv.put(RUA, clientes.getRua());
    cv.put(BAIRRO, clientes.getBairro());
    cv.put(EMAIL, clientes.getEmail());
    cv.put(TELEFONE, clientes.getTelefone());
    cv.put(CELULAR, clientes.getCelular());

    db.getWritableDatabase().insert(TABLE_NAME, null, cv);
}


//Método setado no onClick da minha tela de cadastros
public void gravarNoBanco(){
    cd.insert(new Clientes(0, edit_nome.getText().toString(),edit_rua.getText().toString(),edit_email.getText().toString(),edit_bairro.getText().toString(),edit_telefone.getText().toString(),edit_celular.getText().toString()));
    clear();
}
  • Whenever you change the database you have to uninstall the application or increase its version in the class it inherits from Sqliteopenhelper

  • That was the end of it.obg.

2 answers

1


As far as I can tell, response of Sachin Garg for a similar question in English can solve your problem too:

It looks like you added some columns later in the database. You should consider uninstalling and reinstalling your application. A better approach is to delete (drop) and recreate all tables in the method onUpdate, and increment the database version every time you modify the schema.

0

Increase the version of your database and implement the following method

@Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        //codigo para atualizar suas tabelas, alter table por exemplo
    }

Whenever the database version is increased, the onUpgrade method will be called the first time the database connection is instantiated after the version increase

Browser other questions tagged

You are not signed in. Login or sign up in order to post.