Error while changing table SQLITE - no such column

1

Include 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 no path picture column, in my case, in the clients table. I read some things about it and was advised to install the application again and still did not solve it. 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()); 

   ......................
    
asked by anonymous 26.01.2016 / 03:57

1 answer

2

When changing the DB structure it is necessary to inform the SQLiteOpenHelper class (BankData) of this change.

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

So, whenever you change the structure of the DB, you have to instantiate the class BankDados , to increase by 1 the value to pass to parameter version .

What you usually do is declare a constant for the version number and when you change the code to generate the BD, you change that value as well.

I suggest that you change the BankData class 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);
    }
    .....
    .....
}

You must reflect the builder change in all places where you create a BankDados instance. You will find that it has become simpler to instantiate the BancoDados class and that when you have to change the version it is only done in one place.

    
26.01.2016 / 12:16