Skip to content
Advertisement

How to access a local, pre-populated sqlite database using Kotlin in Android Studio?

For a personal project, I am creating a quiz app written in Kotlin. To store information about a given question (four choices, the question itself, a category, and the correct answer), I have created a database using SQLite that stores said information.

However, I am not unsure of a couple of things. One is where do I put my prepopulated database in android studio, and where does this database then show up in an android file explorer.

Then, assuming the database is in a local file and I have access to it, how do I create a SQLite Db handler method that allows me to access information about a single row. I’ve tried using SQLiteOpenHelper with no luck. Every time I try to access a row, the table is empty, even though I have already created a table. Furthermore, I’ve looked into ROOM and it does not seem to support accessing local, pre-populated files.

What approach should I take and how would I implement this approach? An answer to any of my questions is appreciated.

Advertisement

Answer

Furthermore, I’ve looked into ROOM and it does not seem to support accessing local, pre-populated files.

ROOM solves your solution, From the Android Docs:

To prepopulate a Room database from a prepackaged database file that is located anywhere in your app’s assets/ directory, call the createFromAsset() method from your RoomDatabase.Builder object before calling build():

Room.databaseBuilder(appContext, AppDatabase.class, "Sample.db")
.createFromAsset("database/myapp.db")
.build()
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement