Error changing table SQLITE - in such column

Asked

Viewed 777 times

2

I included a new column in a table. I reinstalled the application and at the time of saving the data there is a message saying that there is a pathImage column, in my case in the clients table. I read some things about it and I was advised to install the application again and it still hasn’t solved. I’m trying to make a ALTER TABLE but maybe I’m doing it wrong. I ask for help. Thank you.

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import static br.gestaoBd.Login.db;
import static java.lang.Integer.TYPE;

public class BancoDados extends SQLiteOpenHelper {

public BancoDados(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db) {
    EstruturaBanco.criarTabelas();
}

@Override
public void onUpgrade(SQLiteDatabase sqld, int i, int i1) {

    db.execSQL("ALTER TABLE [clientes] ADD COLUMN [pathImagem2] VARCHAR2(125) NOT NULL  ");
}
}  

Bank structure

 import br.gestaoBd.Login;



public class EstruturaBanco {

public static void criarTabelas() {
    System.out.println("Criando as tabelas...");
    StringBuilder sb = new StringBuilder();
    sb.append(" CREATE TABLE IF NOT EXISTS [clientes] (");
    sb.append(" [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,");
    sb.append(" [nome] VARCHAR2(70) NOT NULL,");
    sb.append(" [email] VARCHAR2(70) NOT NULL,");
    sb.append(" [telefone] VARCHAR2(30) NOT NULL,");
    sb.append(" [endereco] VARCHAR2(70) NOT NULL,");
    sb.append(" [rg] VARCHAR2(30) NOT NULL,");
    sb.append(" [cpf] VARCHAR2(30) NOT NULL,");
    sb.append(" [pathImagem2] VARCHAR2(125) NOT NULL);");
    Login.db.execSQL(sb.toString()); 

   ......................

1 answer

2


When changing the structure of the BD it is necessary to inform the Sqliteopenhelper(Bank) class of that change.

The mechanism it uses is to check the value of the parameter version passed in the constructor with the existing BD version number.
If it is superior it executes the method onUpgrade() if lower performs the method onDowngrade().

Thus, whenever the BD structure changes, it has, when instantiating the class Codless, to increment the value to be passed to the parameter by 1 version.

What is usually done is to declare a constant for the version number and when you change the code to generate the BD this value is also changed.

I suggest you change the class Codless as follows:

public class BancoDados extends SQLiteOpenHelper {

    //Constante para a versão
    private static final int DATABASE_VERSION = 1;

    //O nome da BD não muda, declare uma constante
    private static final String DATABASE_NAME = "NomeBanco";

    //É possível agora simplificar o construtor
    public BancoDados(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    .....
    .....
}

It must reflect the change of the manufacturer in all places where it creates an instance of Codless.
You will find that it has become simpler to instantiate the class Codless and that when you have to change the version it is only done in one place.

Browser other questions tagged

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