Hello, I am trying to get data from the asset folder SQL DB but it showing this error.
java.lang.IllegalStateException: Pre-packaged database has an invalid schema: recipetype(com.example.finddishrecipe.room.RecipeEntity).
Expected:
TableInfo{name='recipetype', columns={name=Column{name='name', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='undefined'}, image=Column{name='image', type='TEXT', affinity='2', notNull=true, primaryKeyPosition=0, defaultValue='undefined'}, id=Column{name='id', type='INTEGER', affinity='3', notNull=true, primaryKeyPosition=1, defaultValue='undefined'}}, foreignKeys=[], indices=[]}
Found:
TableInfo{name='recipetype', columns={id=Column{name='id', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1, defaultValue='undefined'}, name=Column{name='name', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}, image=Column{name='image', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0, defaultValue='undefined'}}, foreignKeys=[], indices=[]}```
<brThis is my entity code/>
@Entity(tableName = "recipetype")
data class RecipeEntity(
@PrimaryKey
@ColumnInfo(name = "id")
val id: Int,
@ColumnInfo(name = "name")
val name: String,
@ColumnInfo(name = "image")
val image: String)
2
Answers
All the columns are supposed to have non null constraints according to expected schema. Define them as non null values while creating the table by adding
NOT NULL
constraint as follows:Or you can make entity class columns as nullables.
Order of the columns shouldn’t matter.
The simplest way to ensure that a prepackaged database matches what Room expects is to:-
@Entity
annotated class(es), then@Database
annotated class (abstract) with the@Entity
annotated class(es) defined in the entities parameter of the@Database
annotation, then@Database
annotated class but suffixed with Impl, then@Entity
annotated class)If you have an existing database that is relatively close then you can use the SQLite Tool to convert the definitions along the lines of:-
INSERT INTO actual_table SELECT * FROM renamed_existing_table
obviously naming the table names accordingly)