Avadhesh Mourya
Avadhesh Mourya

Reputation: 239

How do I delete column from table in room database?

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

    }
};

Upvotes: 1

Views: 3876

Answers (1)

Johnny
Johnny

Reputation: 2573

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

Upvotes: 1

Related Questions