androiddatabasekotlinroom

Kotlin andoid pre poluate data base


I am trying to build a really simple db to store the score of a Hangman game.

I built an entity :

@Entity(tableName = "scores") data class DBScore( @PrimaryKey var pseudo: String = "", var score: Int = 0 ) {

}

//Conversion method to go from DB model to kotlin model fun List.asDomainModel(): List { return map { Score( pseudo = it.pseudo, score = it.score ) }

}

To prefill the db with sample values with a db file

 INSTANCE = databaseBuilder(
                    context.applicationContext,
                    AppDataBase::class.java,
                    "app_database"
                )
                    .createFromAsset("database/scores.db").build()

I have created the following db file to do so : https://wetransfer.com/downloads/c534dd62136c94f17c0ee71f1ef39a1920201011144837/6ff2b7

But I get an error and don't understand what is wrong with my db :

Caused by: java.lang.IllegalStateException: Pre-packaged database has an invalid schema: scores(com.example.hangmangame.database.entity.DBScore). Expected: TableInfo{name='scores', columns={pseudo=Column{name='pseudo', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}, score=Column{name='score', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=0, defaultValue='null'}}, foreignKeys=[], indices=[]} Found: TableInfo{name='scores', columns={score=Column{name='score', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0, defaultValue='null'}, pseudo=Column{name='pseudo', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=1, defaultValue='null'}}, foreignKeys=[], indices=[]}

Seems to be an issue with the order of the columns but for me SQL is not sensitive to it.

Thx for any help.

Regards

Quentin.


Solution

  • Room is expecting score to be NOT NULL. Your pre-populated database schema has it as NULL, apparently, based on the error.