I am building a Quiz app to practice what I've learned so far. I want to retrieve data from the sql lite. Entering data in a non embedded RDMBS is trivial. With SQL lite I just don't know what to do. What steps you guys suggest? I am using Room database.
-
1`ROOM` now supports the pre-populated databases. use google official document [this](https://developer.android.com/training/data-storage/room/prepopulate) – Kapta Mar 07 '20 at 15:18
1 Answers
Room 2.2.0 and higher has built-in support for initializing your database from an existing database.
So, if you wanted to hand-populate your database, roughly speaking, the steps would be:
Set up your Room entities and
RoomDatabase
in your appRun your app and have Room create an empty database for you
Copy that database off of your device or emulator (e.g., using Android Studio's Device File Explorer tool)
Use a SQLite client to add rows to your tables with the proper data
Put that database in
assets/
within your projectModify your
RoomDatabase
setup to usecreateFromAsset()
If it is more convenient for you to maintain your data in some other format (JSON, XML, CSV, whatever), you have two main choices:
Generate the Room database from that data on your development machine (e.g., via a custom Gradle task) and package the database, or
Package the raw data (e.g., JSON) in your app, parse that, and use that to populate your database
For example, in this sample app I wanted to demonstrate Room's full-text search (FTS) support. I wanted to use the contents of a book, and that was going to be easier to maintain as plain text files. So, I package the plain text files in the app and populate the Room database from there.
(I am hoping to have a createFromAsset()
sample in the future)

- 986,068
- 189
- 2,389
- 2,491
-
I followed your structions and all the steps went as smooth as could be. Thanks for your help. – Diego Alves Mar 08 '20 at 15:30