-2

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.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Diego Alves
  • 2,462
  • 3
  • 32
  • 65
  • 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 Answers1

1

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:

  1. Set up your Room entities and RoomDatabase in your app

  2. Run your app and have Room create an empty database for you

  3. Copy that database off of your device or emulator (e.g., using Android Studio's Device File Explorer tool)

  4. Use a SQLite client to add rows to your tables with the proper data

  5. Put that database in assets/ within your project

  6. Modify your RoomDatabase setup to use createFromAsset()

If it is more convenient for you to maintain your data in some other format (JSON, XML, CSV, whatever), you have two main choices:

  1. Generate the Room database from that data on your development machine (e.g., via a custom Gradle task) and package the database, or

  2. 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)

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491