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

Asked

Viewed 499 times

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");
}

2 answers

4

This is because you added a new table without changing the version of the database.

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

But as your project should be in development it is still advisable that you uninstall before running again,

  • So Leonardo, I’ve done it. But you keep presenting the same problem.

  • You uninstalled the device application and ran again?

  • Exactly. I did that and I changed the bank version too.

3

To keep you from always falling onUpgrade, make a test, 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);
    }
}

Browser other questions tagged

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