Questions tagged [android-room-prepackageddatabase]

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

29 questions
76
votes
8 answers

How to use Room Persistence Library with pre-populated database?

I'd like to use Room with a pre-populated database, but I can't understand how to tell Room where to find my database. I've now put it in src/main/assets/databases and when I create the instance for the Room database I create it this…
62
votes
7 answers

How to populate Android Room database table on first run?

In SQLiteOpenHelper there is a onCreate(SQLiteDatabase ...) method which i used to populate database tables with some initial data. Is there a way to insert some data into Room database table on first app run?
11
votes
5 answers

Pre-packaged database has an invalid schema error

I'm building an Android application based on an old Android project. In my new application I'm using Room. I have to use the same database that is used in the first project. Furthermore, I've extracted the database from the first project using…
4
votes
1 answer

Is it normal for Android Room to be storing the prepackaged database in cache?

I'm currently creating an Android application that uses a prepackaged database. The database is initially around 40MB in size and is stored in the assets/databases folder. The Problem When I look up how much space my application is using, the "App…
4
votes
1 answer

Android Room migration with updated pre-populated database

I'm having a little bit of a headache with Room and migration with a pre-populated database. EXPLANATION I'm currently using Room and a pre-populated database. With the first version (version 1) the database loads fine and everything works…
4
votes
1 answer

createFromAsset and fallbackToDestructiveMigration methods of Room library - no data exists

I have a .db file in assets folder. My RoomDatabase class is as the following. I install the app to my device. Then I changed version = 2 in the below class, and make my prepopulated database version 2. Then i renamed one of the columns in my table…
metis
  • 1,024
  • 2
  • 10
  • 26
3
votes
1 answer

Room.createFromAsset() rewrite app's database every time the app is opened

I am trying to initialize my app's database from an asset database when it first installed. However, it works fine until the app is closed. When I open the app again it calls createFromAsset(). As far as I know, the problem is in the fact that I'm…
3
votes
1 answer

How to use createFromAsset function from Room library?

Room persistence library version 2.2.0-alpha01 has added the ability to use pre-packaged databases. https://developer.android.com/jetpack/androidx/releases/room Can someone provide an example for how to initialize the room database builder?
syloc
  • 4,569
  • 5
  • 34
  • 49
2
votes
1 answer

Room create from asset + fallbackToDestructiveMigration recreating the database on each request

Using Room, I load my database from assets (an already populated database). I do plan to update the database content very often, so every time the version is increased, the old database the user has installed in their storage must be removed and…
2
votes
1 answer

Room with prepopulated database

In my app I'm using Room with a prepopulated database myDatabase.db which is then accessed using this code: private val database = Room .databaseBuilder(context.applicationContext, AppDatabase::class.java, dbName) …
2
votes
1 answer

How to add new data to android room database when updating app?

I'm making an android app with Room database. My plan is to prepopulate database with some initial data when it is installed on device, and user can edit it and insert new row on each table. New row id by users will start from, for example,…
2
votes
2 answers

Cannot pre-populate room database when changing schema

I am at my wits end with a problem I cannot resolve - when trying to change the schema/data of a table in my database. I am pre-populating the database with Rooms .createFromAsset method. Current schema and data (working) - as shown in DB Browser -…
2
votes
2 answers

Why database is null, when i use createFromAsset in RoomDatabase. List<> from it has size 0, but prepopulated database has 8 items

This is my Database. @Database(entities = {Word.class}, version = 1, exportSchema = false) public abstract class WordsDatabase extends RoomDatabase { private static final String DB_NAME = "words"; private static WordsDatabase…
1
vote
0 answers

Android Room: Query returns null in Embedded field

Hi I am using Room DB and inserting a primary key and two embedded fields in a table. While trying to get the data from that table. That embedded fields returning null. Table be like: @NonNull @PrimaryKey @ColumnInfo(name = "ID",…
1
vote
1 answer

Room Pre-packaged database has an invalid schema ERROR of Expect type=Integer Found=Boolean

I'm a bit lost with this issue. The Pre-packaged database has an invalid schema error has the following output: Expected TableInfo{name='account', columns={client_alt_phone_on_route_sheets=Column{name='client_alt_phone_on_route_sheets',…
1
2