Error android.database.sqlite.SQLiteException: no such table: fs_promocoes

2
public class PromocaoDAO  {

private static final String DATABASE_NAME = "app_promocoes.db";
private static final int VERSAO_BANCO = 2;

private static final String TABLE_PROMOCOES = "fs_promocoes";

public static final String PROMO_ID = "id";
public static final String PROMO_DESCRICAO = "descricao";
public static final String PROMO_STATUS = "status";
public static final String PROMO_INICIO = "inicio";
public static final String PROMO_TERMINO = "termino";

private DatabaseHelper dbHelper;
private SQLiteDatabase db;

private final Context mCtx;

private static final String TABELA_PROMO =
        "CREATE TABLE if not exists " + TABLE_PROMOCOES + " (" +
                PROMO_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
                PROMO_DESCRICAO + "," +
                PROMO_INICIO + "," +
                PROMO_TERMINO + "," +
                PROMO_STATUS + "," +
                " UNIQUE (" + PROMO_ID +"));";


private static class DatabaseHelper extends SQLiteOpenHelper {

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, VERSAO_BANCO);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TABELA_PROMO);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_PROMOCOES);
        onCreate(db);
    }
}

public PromocaoDAO(Context ctx){
    this.mCtx = ctx;
}

public PromocaoDAO open() throws SQLException{
    dbHelper = new DatabaseHelper(mCtx);
    db = dbHelper.getWritableDatabase();
    return this;
}

public void close(){
    if(dbHelper != null){
        dbHelper.close();
    }
}

public long inserirPromocao(String descricapo, String inicio, String termino){

    ContentValues values = new ContentValues();
    values.put(PROMO_DESCRICAO, descricapo);
    values.put(PROMO_INICIO, inicio);
    values.put(PROMO_TERMINO, termino);
    values.put(PROMO_STATUS, 1);

    return db.insert(TABLE_PROMOCOES, null, values);
}

public Cursor buscarTodasPromocoes(){

    Cursor mCursor;
    mCursor = db.query(TABLE_PROMOCOES, new String[] {PROMO_DESCRICAO, PROMO_INICIO, PROMO_TERMINO},null,null,null,null,null);
    if (mCursor != null){
        mCursor.moveToFirst();
    }
    return mCursor;
}

public void testeInsertPromocao(){
    inserirPromocao("PROMOCAO DE NATAL", "10/11/2016", "31/12/2016");
    inserirPromocao("PROMOCAO DE PESCOA", "10/11/2016", "31/12/2016");
}
    
asked by anonymous 09.11.2016 / 18:36

2 answers

4

This happens because you added a new table without changing the bank version.

You can uninstall the application and run it again to resolve or just change the version of the database.

But since your project is still under development, it is still advisable that you uninstall before running again,

    
09.11.2016 / 18:38
3

To prevent it from ever falling into onUpgrade , test it by inserting a condition that occurs only if you make the database version change ( VERSAO_BANCO ):

    @Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
    if (oldVersion < newVersion){
        String sql = "DROP TABLE IF EXISTS " + TABLE_PROMOCOES;
        database.execSQL(sql);
        onCreate(database);
    }
}
    
09.11.2016 / 18:59