skip to Main Content

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


  1. 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:

    CREATE TABLE "recipetype" ( "id" INTEGER NOT NULL, "name" TEXT NOT NULL, "image" TEXT NOT NULL, PRIMARY KEY("id" AUTOINCREMENT) )
    

    Or you can make entity class columns as nullables.

    Order of the columns shouldn’t matter.

    Login or Signup to reply.
  2. The simplest way to ensure that a prepackaged database matches what Room expects is to:-

    1. Create the @Entity annotated class(es), then
    2. Add the @Database annotated class (abstract) with the @Entity annotated class(es) defined in the entities parameter of the @Database annotation, then
    3. Successfully compile the project (CTRL +F9).
    4. Using the Android View locate the java(generated) folder, then
    5. Locate the class that is the same name as the @Database annotated class but suffixed with Impl, then
    6. Locate the createAllTables method, then for each table (@Entity annotated class)
    7. copy the SQL that Room has generated for the table to create the table in the SQLite Tool you are using.
      1. this SQL is EXACTLY what Room Expects and hence there is no need to try to predict Room’s underlying rules.
    • Note ignore room_master table, Room will handle it’s creation
    • other components, such as Views and Indexes can also be created accordingly as and if required.

    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:-

    1. ALTER the existing table(s) to RENAME it(them).
    2. CREATE the actual table(s) using the SQL from the generated java.
    3. Use the renamed original table to load the data into the newly created actual table (often this could be as simple as INSERT INTO actual_table SELECT * FROM renamed_existing_table obviously naming the table names accordingly)
    4. Optionally DROP the renamed table(s)
    5. Optionally VACUUM the database (to free unused pages)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search