For questions about using pre-packaged aka pre-populated databases with Android Room so that an Android App can be installed with a database that is included as part of the install, rather than being built over time from an empty database.
Android Room caters for pre-populated or pre-packaged databases via a call to either createFromAsset
or createFromFile
.
Such a call facilitates the copy of an SQLite database, created externally, instead of Room creating an empty database, thus allowing a data rich database to exist from when the database is first accessed.
A prepackageddatabase database must have a schema that is as Room expects, which is as per the classes that are annotated with @Entity. If a pre-packaged database does not meet the expectations then a failure will result.
A convenient method of ensuring that the database does meet Room's expectation is to let Room generate what it expects. That is create the classes annotated with @Entity
(each will define a table), create the class annotated with @Database
, including the @Entity
classes in the entities=
parameter of the @Database
annotation and then compiling.
The SQL for creating the tables can be found in the Java(generated) (visible via the Android view in Android Studio) in the class that is name the same as the @Database class but suffixed with _Impl
. The SQL can then be copied and pasted into an SQLite tool and the tables will be as required and read for populating.
The populated database can then be copied into the Project (typically into the assets folder).
You may wish to check-out Prepopulate your Room database