Complete the code to define a Room database version.
val db = Room.databaseBuilder(context, AppDatabase::class.java, "app_db") .version([1]) .build()
The initial version of a Room database is usually set to 1.
Complete the code to add a migration from version 1 to 2.
val MIGRATION_1_2 = object : Migration(1, 2) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL([1]) } }
To migrate from version 1 to 2, we add a new column 'age' with a default value.
Fix the error in the migration code to rename a table.
val MIGRATION_2_3 = object : Migration(2, 3) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE users RENAME TO [1]") } }
To rename the table 'users' to 'users_old', the new name must be specified correctly.
Fill both blanks to create a migration that creates a new table.
val MIGRATION_3_4 = object : Migration(3, 4) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("CREATE TABLE [1] (id INTEGER PRIMARY KEY, name TEXT)") database.execSQL("INSERT INTO [2] (id, name) VALUES (1, 'Alice')") } }
Both blanks must use the same table name 'employees' to create and insert data correctly.
Fill all three blanks to define a migration that adds a new column and updates data.
val MIGRATION_4_5 = object : Migration(4, 5) { override fun migrate(database: SupportSQLiteDatabase) { database.execSQL("ALTER TABLE [1] ADD COLUMN [2] INTEGER DEFAULT 0") database.execSQL("UPDATE [3] SET [2] = 10 WHERE id = 1") } }
The migration adds a new column 'score' to the 'users' table and updates it for a specific row.
