How to preload Room Database with prepackaged DB ๐Ÿ—ƒ

Sree Kumar A.V
2 min readApr 19, 2021

Why use Room?

1. It is annotation-driven. ๐Ÿ“

2. It's easy to configure. ๐Ÿ•น

3. There is less code to write. โœ‚๏ธ

4. No need to worry about threading. ๐Ÿงถ

5. It has Rx support and co-routine support. ๐ŸŽ—

6. It's very easy to unit test. ๐Ÿงช

7. You can use room to preload DB from a file ๐Ÿ—ƒ

8. There is real-time editing via database inspector on Android Studio. ๐Ÿ”

TODOโ€™s Before Preloading Table with Prepackaged database

When preloading data onto DB:

  • Room will do an integrity check before loading the tables with contents.
  • Ensure youโ€™re following the rules below:

1. Data types are supported โœ…
The data type should match supported SqlLite types:

NULL: The value is a NULL value.

INTEGER: The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

REAL: The value is a floating-point value, stored as an 8-byte IEEE floating-point number.

TEXT: The value is a text string, stored using the database encoding (UTF-8, UTF-16BE or UTF-16LE).

BLOB: The value is a blob of data, stored exactly as it was input.

2. Column count & names match โœ…

What may seem obvious to some is not to others. One canโ€™t add/remove columns without a migration even when preloading data for the first time.

3. Verify the validity โœ…

When opening the database Room will check the integrity of your table using RoomOpenHelper.java class.

Notice within the method checkIdentity()

Ensures proper usage. If any one of the above rules is not met, then it will throw the following exception:

Thanks & Happy Coding !!

--

--