简体   繁体   中英

Room database hangs after adding new table and updating version number

As soon as my App executes the first room command (in a different table), the App just freezes.

The last message I get from the Console is:

I/SQLiteOpenHelper: DB version upgrading from 3 to 4

I thought I correctly upgraded the DB with the Migration command. I also see a file 4.json , containing the newly created entity.

MainDatabase.kt

@Database(
    entities = [
        PaymentEntity::class,
        PaymentOptionsEntity::class,
    exportSchema = true,
    version = 4
)
abstract class MainDatabase : RoomDatabase() {
abstract fun paymentDao(): PaymentDao

    companion object {
        @Volatile
        private var INSTANCE: MainDatabase? = null

        val MIGRATION_3_4 = object : Migration(3, 4){
            override fun migrate(database: SupportSQLiteDatabase) {
                database.execSQL("CREATE TABLE IF NOT EXISTS `PaymentOptionsEntity` (`id` INTEGER, PRIMARY KEY(`id`))")
            }
        }

        fun getDatabase(
            context: Context,
            scope: CoroutineScope? = null
        ): MainDatabase {
            // if the INSTANCE is not null, then return it,
            // if it is, then create the database
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    MainDatabase::class.java,
                    BuildConfig.APPLICATION_ID + "_" + BuildConfig.FLAVOR + "_db"
                )
                    .addMigrations(MIGRATION_1_2, MIGRATION_2_3, MIGRATION_3_4)
                    .build()
                INSTANCE = instance
                // return instance
                instance
            }
        }    
    }
}

PaymentDao

@Dao
interface PaymentDao {
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insertPaymentOptions(paymentOptions: List<PaymentOptionsEntity>)

    @Query("SELECT * FROM PaymentOptionsEntity")
    suspend fun getPaymentOptions(): List<PaymentOptionsEntity>
}

PaymentOptionsEntity

@Parcelize
@Entity(tableName="PaymentOptionsEntity")
data class PaymentOptionsEntity (
    @PrimaryKey
    val id: String = ""
): Parcelable

build.gradle (app):

implementation "androidx.room:room-runtime:2.4.0-alpha03"
implementation "androidx.room:room-ktx:2.4.0-alpha03"

You have defined id as String in PaymentOptionsEntity

and in MIGRATION_3_4 as Integer ("CREATE TABLE IF NOT EXISTS PaymentOptionsEntity ( id INTEGER, PRIMARY KEY( id ))")

keep same in both places

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM