1

How do I delete column from table in room database android.I alredy try all the way drop and delete not working.

This code also not working:-

static final Migration MIGRATION_3_4 = new Migration(3, 4) 

{
    @Override
    public void migrate(SupportSQLiteDatabase database) 
{

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

    }
};
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Avadhesh Mourya
  • 229
  • 1
  • 3
  • 8

1 Answers1

1

Android ROOM is based on SQL Lite which does not allow dropping database table columns. Instead you have to:

  • create a temporary table without the column you are trying to delete,
  • copy all records from old table to new table,
  • drop the old table,
  • rename new table to same name as old table

See related question:Sqlite Dropping Column from table

Johnny
  • 2,503
  • 6
  • 21
  • 22