Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete column in next version of room database Android?

How to delete a column in next version of room database android.DROp or delete not working.?

@Database(entities = {User.class, AdTime.class}, version = 1, exportSchema = false)
  public abstract class AppDataBase extends RoomDatabase {

  private static AppDataBase INSTANCE;

  public abstract UserDao userModel();

  public abstract AdDao adModel();

  public static AppDataBase getInMemoryDatabase(Context context) {

    if (INSTANCE == null) {
        INSTANCE = Room.databaseBuilder(context, AppDataBase.class, "adrider")
                // To simplify the codelab, allow queries on the main thread.
                // Don't do this on a real app! See PersistenceBasicSample for an example.
                .allowMainThreadQueries()
              //  .addMigrations(MIGRATION_1_2)
                //  .fallbackToDestructiveMigration()
                .build();
      }
      return INSTANCE;
    }

  static final Migration MIGRATION_1_2=new Migration(1,2) {
      @Override
      public void migrate(@NonNull SupportSQLiteDatabase database) {
          database.execSQL("ALTER TABLE User "
                + "DROP Download");
      }
  };

}
like image 608
Avadhesh Mourya Avatar asked Sep 08 '18 12:09

Avadhesh Mourya


People also ask

How do I remove an item from a room database?

3.1 Add the Clear all data menu option In the Options menu, select Clear all data. All words should disappear. Restart the app. (Restart it from your device or the emulator; don't run it again from Android Studio) You should see the initial set of words.

How do you delete a column of data?

Right-click the column you want to delete and choose Delete Column from the shortcut menu. If the column participates in a relationship (FOREIGN KEY or PRIMARY KEY), a message prompts you to confirm the deletion of the selected columns and their relationships. Choose Yes.

How do I delete a column in a table?

Right-click in a table cell, row, or column you want to delete. On the Mini toolbar, click Delete. Choose Delete Cells, Delete Columns, or Delete Rows.

When should I change the room database version?

More precisely, every time you alter your schema by adding, removing or modifying tables, you have to increase the database version number and update your implementation of SQLiteOpenHelper. onUpgrade method. This is how you tell SQLite what it needs to do when going from an old version to a new version.


2 Answers

You have to do 4 steps:
1. Create the new table
2. Copy the data
3. Remove the old table
4. Change the table name to the correct one

static final Migration MIGRATION_1_2 = new Migration(1, 2) {

    @Override
    public void migrate(SupportSQLiteDatabase database) {
        // Create the new table
        database.execSQL(
                "CREATE TABLE users_new (userid TEXT, username TEXT, last_update INTEGER, 
        PRIMARY KEY(userid))");
        // Copy the data
        database.execSQL(
                "INSERT INTO users_new (userid, username, last_update) SELECT userid, 
        username, last_update FROM users");
        // Remove the old table
        database.execSQL("DROP TABLE users");
        // Change the table name to the correct one
        database.execSQL("ALTER TABLE users_new RENAME TO users");
    }

};
like image 54
MrVasilev Avatar answered Sep 21 '22 02:09

MrVasilev


My answer is very similar to what @Pedro Romao has given except that I have removed the extra unnecessary "NOT NULL" included in the sql statements. It should be added only where necessary as it might even cause an error where a column is allowed to be Null.

    // Create a table that would be your new table. Specify the type of each field and include NON NULL when field can't be null for example in the case of a primary key
    database.execSQL("CREATE TABLE Users_backup (id INTEGER NOT NULL, name TEXT, PRIMARY KEY(id))"); 
    // Copy the desired columns from the old table into this new table
    database.execSQL("INSERT INTO Users_backup (id, name) SELECT id, name FROM Users");
    // Delete the old table
    database.execSQL("DROP TABLE Users");
    // Rename the new table to the old table's name so that the rest of your code can recognize this table as the former one.
    database.execSQL("ALTER TABLE Users_backup RENAME TO Users");
like image 29
Njuacha Hubert Avatar answered Sep 20 '22 02:09

Njuacha Hubert