Deleted data in SQLite android

3

I had a problem with my bdd in android apparently everything is due to how I have structured the part of onUpgrade

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

On some occasions the whole table is deleted, as I could do so that this never happens and avoid loss of information that really needs to be saved.

  

Full Code of my database class

public class BasedeDatos_AgroMovil extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "agromovil.db";
    public static final String TABLE_CAJAS = "cajas";

//constructor
    public BasedeDatos_AgroMovil(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
            super(context, DATABASE_NAME, factory, DATABASE_VERSION);
        }



@Override
    public void onCreate(SQLiteDatabase db) {
        String TABLA_CAJAS = "CREATE TABLE " + TABLE_CAJAS + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY, " +
                COLUMN_FECHA + " TEXT," +
                COLUMN_RANCHO + " TEXT," +
                COLUMN_SECTOR + " TEXT," +
                COLUMN_TRABAJADOR + " TEXT)";
        db.execSQL(TABLA_CAJAS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAJAS);
        onCreate(db);
    }
}
    
asked by DoubleM 14.02.2017 в 08:35
source

3 answers

4

Defines to update only when the new version is greater than the current version

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    if (newVersion > oldVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAJAS);
        onCreate(db);
    }
}
    
answered by 14.02.2017 / 13:10
source
4

The onUpgrade method is executed when you change the version of the database, that is, when you update it and you are forced to upload its version number (DATABASE_VERSION), either because you add or remove fields in the tables or whatever. In this case, you are deleting the database every time you update your version.

While, the onCreate method is executed when the database is created.

So, what you have to do in case you want to update it and change its version number is:

  • Remove all the data you want to save in the onUpgrade method
  • Delete the table
  • Create the new tables
  • Enter data
  • If you want to do nothing, then you should not change the version. If you want to do something else and change the version number, but not delete it, you should remove the Drop Table , what it does is delete it whenever it exists.

        
    answered by 14.02.2017 в 08:45
    2

    This is true for classes that extend from SQLiteOpenHelper when an update of the application is done the method is called onUpgrade () , this is where you validate if the version is new (versionCode of application), in your case the implementation of this method is doing a DROP of the table:

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

    Depending on your application, there are tables that when updated can be removed and created again, in other cases it is required that these data persist, if you require the same data simply do not call the elimination and creation of the table:

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
       // db.execSQL("DROP TABLE IF EXISTS " + TABLE_CAJAS);
       // onCreate(db);
    }
    
        
    answered by 14.02.2017 в 17:46