How to create prepopulated DB only at first start of android application?

68 views Asked by At

Hellow eweryone! I'm begginer at Room and I need to create DB with starting data at first starting of App and then modify data in app and use modifyed data at second starting of App.

I have this code

companion object {
    @Volatile
    private var INSTANCE: AppDatabase? = null

    fun getDatabase(context: Context): AppDatabase {
        return INSTANCE ?: synchronized(this) {
            val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_database.db",
                )
                    .createFromAsset("database/01-01-2024.db")
                    .build()
        
            INSTANCE = instance

            instance
        }
    }

It creates prepopulated DB successfully and I can modify data in next screen of my app. But when i close the app and open second time the DB with my modified data rewrites with prepopulated DB.

How can i use prepopulated DB only when BD is not exists?

P.S. I was watching documentation but it didn't help.

2

There are 2 answers

0
Pavel Bukhalov On BEST ANSWER

What a shame! I didn't check the clear data checkbox in my run configuration enter image description here

1
MikeT On

According to the code you have supplied then the pre-populated database should, when the App is first run, copy the pre-populated database from the asset to the standard database location and subsequent runs should see that the database exists and then not redo the copy.

  • standard location is in the data/data/<the_package_name>/databases folder/directory
    • <the_package_name> will be according to the actual package name

That is the process is along the lines of:-

  1. does the database exist at the standard location.
  2. if not then copy the asset to the standard location.
  3. open the database at the standard location.

As such your issue is either that the changes are not being applied to the database, or the database is being deleted.

  • if you instead of rerunning the App, uninstall and then run the App, this would result in the database being deleted as it is located in the App's data.

It is impossible to ascertain which scenario is resulting in the issue you are encountering. However perhaps consider the following demonstration that amends the code you have supplied that could be used for debugging and ascertaining the issue:-

First the amended code (see comments) including a guess at the encompassing AppDatabase abstract class that incorporates the retrieval of a single @Dao interface :-

@Database(entities = [TheTable::class], exportSchema = false, version = 1)
abstract class AppDatabase: RoomDatabase() {
    abstract fun getAllDAOs(): AllDAOs
    companion object {
        @Volatile
        private var INSTANCE: AppDatabase? = null

        fun getDatabase(context: Context): AppDatabase {
            /* Amended to inspect the database prior to acess it via Room */
            preDBAccess(context)
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    AppDatabase::class.java,
                    "app_database.db",
                )
                        /* Amended to invoke the PrePackagedDatabaseCallback (just to log when prepackaged database is used) */
                    .createFromAsset("database/01-01-2024.db", prePackagedDatabaseCallback)
                        /* Amended to invoke the standard callbacks (OnCreate,OnOpen,OnDestMig) IF INVOKED */
                    .addCallback(cb)
                        /* Amended to allow running on the main thread for brevity */
                    .allowMainThreadQueries()
                    .build()
                INSTANCE = instance

                instance
            }
        }
        val cb = object: Callback(){
            override fun onCreate(db: SupportSQLiteDatabase) {
                super.onCreate(db)
                Log.d(TAG+"ONCRT","OnCreate Callback invoked!.")
                showDBInfo(db)
            }

            override fun onOpen(db: SupportSQLiteDatabase) {
                super.onOpen(db)
                Log.d(TAG+"ONOPN","OnOpen Callback invoked!.")
                showDBInfo(db)
            }

            override fun onDestructiveMigration(db: SupportSQLiteDatabase) {
                super.onDestructiveMigration(db)
                Log.d(TAG+"ONDM","OnDestructiveMigration invoked!.")
                showDBInfo(db)
            }
        }

        val prePackagedDatabaseCallback = object:PrepackagedDatabaseCallback(){
            override fun onOpenPrepackagedDatabase(db: SupportSQLiteDatabase) {
                super.onOpenPrepackagedDatabase(db)
                Log.d(TAG + "_PPDC","PrePackagedDatabase Callback invoked!.")
                showDBInfo(db)
            }
        }

        fun showDBInfo(db: SupportSQLiteDatabase) {
            var csr = db.query("SELECT * FROM sqlite_master")
            DatabaseUtils.dumpCursor(csr)
            csr = db.query("SELECT * FROM thetable")
            DatabaseUtils.dumpCursor(csr)
            csr.close()
        }
        fun preDBAccess(context: Context) {
            Log.d(TAG+"PDBA","Pre Database Access processing initiated!.")
            val dbFileExists = context.getDatabasePath("app_database.db").exists()
            if (!dbFileExists) {
                Log.d(TAG+"PDBA","Database file app_database.db does not exist! (should be copied from asset)")
            } else {
                Log.d(TAG+"PDBA","Database file app_database.db exists!.")
                val db = SQLiteDatabase.openDatabase(context.getDatabasePath("app_database.db").path,null,0)
                Log.d(TAG+"PDBAV","Database version is ${db.version}")
                var csr = db.rawQuery("SELECT * FROM sqlite_master",null)
                DatabaseUtils.dumpCursor(csr)
                csr = db.rawQuery("SELECT * FROM thetable",null)
                DatabaseUtils.dumpCursor(csr)
                csr.close()
                db.close()
            }
        }
    }
}
  • As can be seen this includes relatively comprehensive debugging (see usage that follows)

Other Room classes used for the demo include:-

const val TAG: String ="DBINFO"
@Entity
data class TheTable(
    @PrimaryKey
    var id: Long?=null,
    var a_column: String
)
@Dao
interface AllDAOs {
    @Insert(onConflict = OnConflictStrategy.IGNORE)
    fun insert(theTable: TheTable): Long
    @Query("SELECT * FROM thetable")
    fun getAllFromTheTable(): List<TheTable>
}

To actually demonstrate some activity code (note main thread used for brevity):-

class MainActivity : AppCompatActivity() {
    lateinit var db: AppDatabase
    lateinit var dao: AllDAOs
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        db = AppDatabase.getDatabase(this)
        dao = db.getAllDAOs()
        /* NOTE database has NOT been accessed yet so no copy undertaken */
        for (tt in dao.getAllFromTheTable()) {
            Log.d(TAG+"ADBA","TT Row ID=${tt.id} a_column=${tt.a_column}")
        }
        for (i in 1..10) {
            dao.insert(TheTable(null,"INSRT_${i} at ${System.currentTimeMillis()}"))
        }
        for (tt in dao.getAllFromTheTable()) {
            Log.d(TAG+"AINSRT","TT Row ID=${tt.id} a_column=${tt.a_column}")
        }
    }
}

This will, when the App is run :-

  1. Construct an AppDatabase object as db
    1. Writing the results from the preDBAccess call to the log
      1. this will either be that the database file does or does not exist
      2. if the database file does exist, and thus the pre-packaged-database will not be copied, then database information (the schema,version and contect of TheTable) will be written to the log.
  2. Retrieve an AllDAOs object as dao
    1. NOTE as this is when the database will be accessed/opened by Room then the respective logging will take place
  3. Extract all rows from the TheTable table and write the extracted data to the log.
  4. Insert 10 rows into the TheTable table
  5. Again extract all the rows TheTable table writing the extracted data to the log (the expectation being that there are 10 additional rows of data)

Example use of the demo

First the above is compiled, the generated Java is inspected and the expected SQL copied for the creation of the table the pre-packaged database to be constructed.

An SQLite tool (Navicat in this case) was used to create the pre-packaged database SQLite database. The SQL used being:-

DROP TABLE IF EXISTS thetable;
CREATE TABLE IF NOT EXISTS `TheTable` (`id` INTEGER, `a_column` TEXT NOT NULL, PRIMARY KEY(`id`));
INSERT INTO thetable (a_column) VALUES ('PPDB ROW 1'),('PPDB ROW 2'),('PPDB ROW 3');
PRAGMA user_version;
PRAGMA user_version=1;
PRAGMA user_version;

Thus the database solely contains:-

enter image description here

The database/connection and Navicat are closed and the file soanswers.db is copied:-

enter image description here

The file is pasted into the assets/database folder and renamed to 01-01-2024.db:-

enter image description here

  • as can be seen the original soanswers.db file has been retained just in case

At this stage testing can be undertaken.

Test 1 - The initial run

The App is run as a fresh install, the log includes:-

2024-03-22 10:58:35.109 D/DBINFOPDBA: Pre Database Access processing initiated!.
2024-03-22 10:58:35.110 D/DBINFOPDBA: Database file app_database.db does not exist! (should be copied from asset)


2024-03-22 10:58:35.248 D/DBINFO_PPDC: PrePackagedDatabase Callback invoked!.


2024-03-22 10:58:35.250 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@d598771
2024-03-22 10:58:35.250 I/System.out: 0 {
2024-03-22 10:58:35.250 I/System.out:    type=table
2024-03-22 10:58:35.250 I/System.out:    name=sqlite_sequence
2024-03-22 10:58:35.251 I/System.out:    tbl_name=sqlite_sequence
2024-03-22 10:58:35.251 I/System.out:    rootpage=3
2024-03-22 10:58:35.251 I/System.out:    sql=CREATE TABLE sqlite_sequence(name,seq)
2024-03-22 10:58:35.251 I/System.out: }
2024-03-22 10:58:35.251 I/System.out: 1 {
2024-03-22 10:58:35.251 I/System.out:    type=table
2024-03-22 10:58:35.251 I/System.out:    name=TheTable
2024-03-22 10:58:35.251 I/System.out:    tbl_name=TheTable
2024-03-22 10:58:35.251 I/System.out:    rootpage=4
2024-03-22 10:58:35.251 I/System.out:    sql=CREATE TABLE `TheTable` (`id` INTEGER, `a_column` TEXT NOT NULL, PRIMARY KEY(`id`))
2024-03-22 10:58:35.251 I/System.out: }
2024-03-22 10:58:35.251 I/System.out: 2 {
2024-03-22 10:58:35.251 I/System.out:    type=table
2024-03-22 10:58:35.251 I/System.out:    name=android_metadata
2024-03-22 10:58:35.252 I/System.out:    tbl_name=android_metadata
2024-03-22 10:58:35.252 I/System.out:    rootpage=5
2024-03-22 10:58:35.252 I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2024-03-22 10:58:35.252 I/System.out: }
2024-03-22 10:58:35.252 I/System.out: <<<<<


2024-03-22 10:58:35.252 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@8c94956
2024-03-22 10:58:35.253 I/System.out: 0 {
2024-03-22 10:58:35.253 I/System.out:    id=1
2024-03-22 10:58:35.253 I/System.out:    a_column=PPDB ROW 1
2024-03-22 10:58:35.253 I/System.out: }
2024-03-22 10:58:35.253 I/System.out: 1 {
2024-03-22 10:58:35.253 I/System.out:    id=2
2024-03-22 10:58:35.253 I/System.out:    a_column=PPDB ROW 2
2024-03-22 10:58:35.253 I/System.out: }
2024-03-22 10:58:35.253 I/System.out: 2 {
2024-03-22 10:58:35.253 I/System.out:    id=3
2024-03-22 10:58:35.253 I/System.out:    a_column=PPDB ROW 3
2024-03-22 10:58:35.253 I/System.out: }
2024-03-22 10:58:35.254 I/System.out: <<<<<


2024-03-22 10:58:35.310 D/DBINFOONOPN: OnOpen Callback invoked!.


2024-03-22 10:58:35.312 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@dbbab30
2024-03-22 10:58:35.313 I/System.out: 0 {
2024-03-22 10:58:35.313 I/System.out:    type=table
2024-03-22 10:58:35.313 I/System.out:    name=sqlite_sequence
2024-03-22 10:58:35.313 I/System.out:    tbl_name=sqlite_sequence
2024-03-22 10:58:35.313 I/System.out:    rootpage=3
2024-03-22 10:58:35.313 I/System.out:    sql=CREATE TABLE sqlite_sequence(name,seq)
2024-03-22 10:58:35.313 I/System.out: }
2024-03-22 10:58:35.313 I/System.out: 1 {
2024-03-22 10:58:35.313 I/System.out:    type=table
2024-03-22 10:58:35.313 I/System.out:    name=TheTable
2024-03-22 10:58:35.313 I/System.out:    tbl_name=TheTable
2024-03-22 10:58:35.314 I/System.out:    rootpage=4
2024-03-22 10:58:35.314 I/System.out:    sql=CREATE TABLE `TheTable` (`id` INTEGER, `a_column` TEXT NOT NULL, PRIMARY KEY(`id`))
2024-03-22 10:58:35.314 I/System.out: }
2024-03-22 10:58:35.314 I/System.out: 2 {
2024-03-22 10:58:35.314 I/System.out:    type=table
2024-03-22 10:58:35.314 I/System.out:    name=android_metadata
2024-03-22 10:58:35.314 I/System.out:    tbl_name=android_metadata
2024-03-22 10:58:35.314 I/System.out:    rootpage=5
2024-03-22 10:58:35.315 I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2024-03-22 10:58:35.315 I/System.out: }
2024-03-22 10:58:35.315 I/System.out: 3 {
2024-03-22 10:58:35.315 I/System.out:    type=table
2024-03-22 10:58:35.315 I/System.out:    name=room_master_table
2024-03-22 10:58:35.315 I/System.out:    tbl_name=room_master_table
2024-03-22 10:58:35.315 I/System.out:    rootpage=6
2024-03-22 10:58:35.315 I/System.out:    sql=CREATE TABLE room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)
2024-03-22 10:58:35.315 I/System.out: }
2024-03-22 10:58:35.315 I/System.out: <<<<<


2024-03-22 10:58:35.316 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@e30b3a9
2024-03-22 10:58:35.317 I/System.out: 0 {
2024-03-22 10:58:35.317 I/System.out:    id=1
2024-03-22 10:58:35.317 I/System.out:    a_column=PPDB ROW 1
2024-03-22 10:58:35.317 I/System.out: }
2024-03-22 10:58:35.317 I/System.out: 1 {
2024-03-22 10:58:35.317 I/System.out:    id=2
2024-03-22 10:58:35.317 I/System.out:    a_column=PPDB ROW 2
2024-03-22 10:58:35.317 I/System.out: }
2024-03-22 10:58:35.317 I/System.out: 2 {
2024-03-22 10:58:35.317 I/System.out:    id=3
2024-03-22 10:58:35.317 I/System.out:    a_column=PPDB ROW 3
2024-03-22 10:58:35.317 I/System.out: }
2024-03-22 10:58:35.317 I/System.out: <<<<<


2024-03-22 10:58:35.321 D/DBINFOADBA: TT Row ID=1 a_column=PPDB ROW 1
2024-03-22 10:58:35.321 D/DBINFOADBA: TT Row ID=2 a_column=PPDB ROW 2
2024-03-22 10:58:35.321 D/DBINFOADBA: TT Row ID=3 a_column=PPDB ROW 3


2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=1 a_column=PPDB ROW 1
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=2 a_column=PPDB ROW 2
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=3 a_column=PPDB ROW 3
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=4 a_column=INSRT_1 at 1711065515321
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=5 a_column=INSRT_2 at 1711065515323
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=6 a_column=INSRT_3 at 1711065515326
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=7 a_column=INSRT_4 at 1711065515327
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=8 a_column=INSRT_5 at 1711065515328
2024-03-22 10:58:35.341 D/DBINFOAINSRT: TT Row ID=9 a_column=INSRT_6 at 1711065515329
2024-03-22 10:58:35.342 D/DBINFOAINSRT: TT Row ID=10 a_column=INSRT_7 at 1711065515331
2024-03-22 10:58:35.342 D/DBINFOAINSRT: TT Row ID=11 a_column=INSRT_8 at 1711065515333
2024-03-22 10:58:35.342 D/DBINFOAINSRT: TT Row ID=12 a_column=INSRT_9 at 1711065515335
2024-03-22 10:58:35.342 D/DBINFOAINSRT: TT Row ID=13 a_column=INSRT_10 at 1711065515337
  • All is as expected, the prepackaged database has been copied and 10 rows added.
    • the pre database access reported that the database did not exist.
    • the onCreate callback was not invoked (the database was copied from the asset)
    • the onOpen callback was invoked.

Test 2 - 2nd Run after the prepackaged database has been copied

The App is rerun (not uninstalled and rerun). The log this time includes:-

2024-03-22 11:05:06.299 D/DBINFOPDBA: Pre Database Access processing initiated!.
2024-03-22 11:05:06.299 D/DBINFOPDBA: Database file app_database.db exists!.
2024-03-22 11:05:06.303 D/DBINFOPDBAV: Database version is 1
2024-03-22 11:05:06.304 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@8962cfb
2024-03-22 11:05:06.304 I/System.out: 0 {
2024-03-22 11:05:06.304 I/System.out:    type=table
2024-03-22 11:05:06.304 I/System.out:    name=sqlite_sequence
2024-03-22 11:05:06.304 I/System.out:    tbl_name=sqlite_sequence
2024-03-22 11:05:06.304 I/System.out:    rootpage=3
2024-03-22 11:05:06.304 I/System.out:    sql=CREATE TABLE sqlite_sequence(name,seq)
2024-03-22 11:05:06.305 I/System.out: }
2024-03-22 11:05:06.305 I/System.out: 1 {
2024-03-22 11:05:06.305 I/System.out:    type=table
2024-03-22 11:05:06.305 I/System.out:    name=TheTable
2024-03-22 11:05:06.305 I/System.out:    tbl_name=TheTable
2024-03-22 11:05:06.305 I/System.out:    rootpage=4
2024-03-22 11:05:06.305 I/System.out:    sql=CREATE TABLE `TheTable` (`id` INTEGER, `a_column` TEXT NOT NULL, PRIMARY KEY(`id`))
2024-03-22 11:05:06.305 I/System.out: }
2024-03-22 11:05:06.305 I/System.out: 2 {
2024-03-22 11:05:06.305 I/System.out:    type=table
2024-03-22 11:05:06.305 I/System.out:    name=android_metadata
2024-03-22 11:05:06.305 I/System.out:    tbl_name=android_metadata
2024-03-22 11:05:06.305 I/System.out:    rootpage=5
2024-03-22 11:05:06.306 I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2024-03-22 11:05:06.306 I/System.out: }
2024-03-22 11:05:06.306 I/System.out: 3 {
2024-03-22 11:05:06.306 I/System.out:    type=table
2024-03-22 11:05:06.306 I/System.out:    name=room_master_table
2024-03-22 11:05:06.306 I/System.out:    tbl_name=room_master_table
2024-03-22 11:05:06.306 I/System.out:    rootpage=6
2024-03-22 11:05:06.306 I/System.out:    sql=CREATE TABLE room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)
2024-03-22 11:05:06.306 I/System.out: }
2024-03-22 11:05:06.306 I/System.out: <<<<<
2024-03-22 11:05:06.307 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@ec7d118
2024-03-22 11:05:06.307 I/System.out: 0 {
2024-03-22 11:05:06.308 I/System.out:    id=1
2024-03-22 11:05:06.308 I/System.out:    a_column=PPDB ROW 1
2024-03-22 11:05:06.308 I/System.out: }
2024-03-22 11:05:06.308 I/System.out: 1 {
2024-03-22 11:05:06.308 I/System.out:    id=2
2024-03-22 11:05:06.308 I/System.out:    a_column=PPDB ROW 2
2024-03-22 11:05:06.308 I/System.out: }
2024-03-22 11:05:06.308 I/System.out: 2 {
2024-03-22 11:05:06.308 I/System.out:    id=3
2024-03-22 11:05:06.308 I/System.out:    a_column=PPDB ROW 3
2024-03-22 11:05:06.308 I/System.out: }
2024-03-22 11:05:06.308 I/System.out: 3 {
2024-03-22 11:05:06.308 I/System.out:    id=4
2024-03-22 11:05:06.308 I/System.out:    a_column=INSRT_1 at 1711065515321
2024-03-22 11:05:06.308 I/System.out: }
2024-03-22 11:05:06.309 I/System.out: 4 {
2024-03-22 11:05:06.309 I/System.out:    id=5
2024-03-22 11:05:06.309 I/System.out:    a_column=INSRT_2 at 1711065515323
2024-03-22 11:05:06.309 I/System.out: }
2024-03-22 11:05:06.309 I/System.out: 5 {
2024-03-22 11:05:06.309 I/System.out:    id=6
2024-03-22 11:05:06.309 I/System.out:    a_column=INSRT_3 at 1711065515326
2024-03-22 11:05:06.309 I/System.out: }
2024-03-22 11:05:06.309 I/System.out: 6 {
2024-03-22 11:05:06.309 I/System.out:    id=7
2024-03-22 11:05:06.309 I/System.out:    a_column=INSRT_4 at 1711065515327
2024-03-22 11:05:06.309 I/System.out: }
2024-03-22 11:05:06.309 I/System.out: 7 {
2024-03-22 11:05:06.309 I/System.out:    id=8
2024-03-22 11:05:06.309 I/System.out:    a_column=INSRT_5 at 1711065515328
2024-03-22 11:05:06.309 I/System.out: }
2024-03-22 11:05:06.310 I/System.out: 8 {
2024-03-22 11:05:06.310 I/System.out:    id=9
2024-03-22 11:05:06.310 I/System.out:    a_column=INSRT_6 at 1711065515329
2024-03-22 11:05:06.310 I/System.out: }
2024-03-22 11:05:06.310 I/System.out: 9 {
2024-03-22 11:05:06.310 I/System.out:    id=10
2024-03-22 11:05:06.310 I/System.out:    a_column=INSRT_7 at 1711065515331
2024-03-22 11:05:06.310 I/System.out: }
2024-03-22 11:05:06.310 I/System.out: 10 {
2024-03-22 11:05:06.310 I/System.out:    id=11
2024-03-22 11:05:06.310 I/System.out:    a_column=INSRT_8 at 1711065515333
2024-03-22 11:05:06.310 I/System.out: }
2024-03-22 11:05:06.311 I/System.out: 11 {
2024-03-22 11:05:06.311 I/System.out:    id=12
2024-03-22 11:05:06.311 I/System.out:    a_column=INSRT_9 at 1711065515335
2024-03-22 11:05:06.311 I/System.out: }
2024-03-22 11:05:06.311 I/System.out: 12 {
2024-03-22 11:05:06.311 I/System.out:    id=13
2024-03-22 11:05:06.311 I/System.out:    a_column=INSRT_10 at 1711065515337
2024-03-22 11:05:06.311 I/System.out: }
2024-03-22 11:05:06.311 I/System.out: <<<<<


2024-03-22 11:05:06.369 D/DBINFOONOPN: OnOpen Callback invoked!.
2024-03-22 11:05:06.371 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@328a4e2
2024-03-22 11:05:06.372 I/System.out: 0 {
2024-03-22 11:05:06.372 I/System.out:    type=table
2024-03-22 11:05:06.372 I/System.out:    name=sqlite_sequence
2024-03-22 11:05:06.372 I/System.out:    tbl_name=sqlite_sequence
2024-03-22 11:05:06.372 I/System.out:    rootpage=3
2024-03-22 11:05:06.372 I/System.out:    sql=CREATE TABLE sqlite_sequence(name,seq)
2024-03-22 11:05:06.372 I/System.out: }
2024-03-22 11:05:06.372 I/System.out: 1 {
2024-03-22 11:05:06.372 I/System.out:    type=table
2024-03-22 11:05:06.372 I/System.out:    name=TheTable
2024-03-22 11:05:06.372 I/System.out:    tbl_name=TheTable
2024-03-22 11:05:06.372 I/System.out:    rootpage=4
2024-03-22 11:05:06.372 I/System.out:    sql=CREATE TABLE `TheTable` (`id` INTEGER, `a_column` TEXT NOT NULL, PRIMARY KEY(`id`))
2024-03-22 11:05:06.372 I/System.out: }
2024-03-22 11:05:06.373 I/System.out: 2 {
2024-03-22 11:05:06.373 I/System.out:    type=table
2024-03-22 11:05:06.373 I/System.out:    name=android_metadata
2024-03-22 11:05:06.373 I/System.out:    tbl_name=android_metadata
2024-03-22 11:05:06.373 I/System.out:    rootpage=5
2024-03-22 11:05:06.373 I/System.out:    sql=CREATE TABLE android_metadata (locale TEXT)
2024-03-22 11:05:06.373 I/System.out: }
2024-03-22 11:05:06.373 I/System.out: 3 {
2024-03-22 11:05:06.373 I/System.out:    type=table
2024-03-22 11:05:06.373 I/System.out:    name=room_master_table
2024-03-22 11:05:06.373 I/System.out:    tbl_name=room_master_table
2024-03-22 11:05:06.373 I/System.out:    rootpage=6
2024-03-22 11:05:06.373 I/System.out:    sql=CREATE TABLE room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)
2024-03-22 11:05:06.373 I/System.out: }
2024-03-22 11:05:06.373 I/System.out: <<<<<
2024-03-22 11:05:06.374 I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@3574073
2024-03-22 11:05:06.374 I/System.out: 0 {
2024-03-22 11:05:06.374 I/System.out:    id=1
2024-03-22 11:05:06.374 I/System.out:    a_column=PPDB ROW 1
2024-03-22 11:05:06.374 I/System.out: }
2024-03-22 11:05:06.374 I/System.out: 1 {
2024-03-22 11:05:06.374 I/System.out:    id=2
2024-03-22 11:05:06.374 I/System.out:    a_column=PPDB ROW 2
2024-03-22 11:05:06.374 I/System.out: }
2024-03-22 11:05:06.375 I/System.out: 2 {
2024-03-22 11:05:06.375 I/System.out:    id=3
2024-03-22 11:05:06.375 I/System.out:    a_column=PPDB ROW 3
2024-03-22 11:05:06.375 I/System.out: }
2024-03-22 11:05:06.375 I/System.out: 3 {
2024-03-22 11:05:06.375 I/System.out:    id=4
2024-03-22 11:05:06.375 I/System.out:    a_column=INSRT_1 at 1711065515321
2024-03-22 11:05:06.375 I/System.out: }
2024-03-22 11:05:06.375 I/System.out: 4 {
2024-03-22 11:05:06.375 I/System.out:    id=5
2024-03-22 11:05:06.375 I/System.out:    a_column=INSRT_2 at 1711065515323
2024-03-22 11:05:06.375 I/System.out: }
2024-03-22 11:05:06.375 I/System.out: 5 {
2024-03-22 11:05:06.375 I/System.out:    id=6
2024-03-22 11:05:06.375 I/System.out:    a_column=INSRT_3 at 1711065515326
2024-03-22 11:05:06.376 I/System.out: }
2024-03-22 11:05:06.376 I/System.out: 6 {
2024-03-22 11:05:06.376 I/System.out:    id=7
2024-03-22 11:05:06.376 I/System.out:    a_column=INSRT_4 at 1711065515327
2024-03-22 11:05:06.376 I/System.out: }
2024-03-22 11:05:06.376 I/System.out: 7 {
2024-03-22 11:05:06.376 I/System.out:    id=8
2024-03-22 11:05:06.376 I/System.out:    a_column=INSRT_5 at 1711065515328
2024-03-22 11:05:06.376 I/System.out: }
2024-03-22 11:05:06.376 I/System.out: 8 {
2024-03-22 11:05:06.376 I/System.out:    id=9
2024-03-22 11:05:06.376 I/System.out:    a_column=INSRT_6 at 1711065515329
2024-03-22 11:05:06.376 I/System.out: }
2024-03-22 11:05:06.376 I/System.out: 9 {
2024-03-22 11:05:06.376 I/System.out:    id=10
2024-03-22 11:05:06.376 I/System.out:    a_column=INSRT_7 at 1711065515331
2024-03-22 11:05:06.376 I/System.out: }
2024-03-22 11:05:06.376 I/System.out: 10 {
2024-03-22 11:05:06.377 I/System.out:    id=11
2024-03-22 11:05:06.377 I/System.out:    a_column=INSRT_8 at 1711065515333
2024-03-22 11:05:06.377 I/System.out: }
2024-03-22 11:05:06.377 I/System.out: 11 {
2024-03-22 11:05:06.377 I/System.out:    id=12
2024-03-22 11:05:06.377 I/System.out:    a_column=INSRT_9 at 1711065515335
2024-03-22 11:05:06.377 I/System.out: }
2024-03-22 11:05:06.377 I/System.out: 12 {
2024-03-22 11:05:06.377 I/System.out:    id=13
2024-03-22 11:05:06.377 I/System.out:    a_column=INSRT_10 at 1711065515337
2024-03-22 11:05:06.377 I/System.out: }
2024-03-22 11:05:06.377 I/System.out: <<<<<


2024-03-22 11:05:06.381 D/DBINFOADBA: TT Row ID=1 a_column=PPDB ROW 1
2024-03-22 11:05:06.381 D/DBINFOADBA: TT Row ID=2 a_column=PPDB ROW 2
2024-03-22 11:05:06.381 D/DBINFOADBA: TT Row ID=3 a_column=PPDB ROW 3
2024-03-22 11:05:06.381 D/DBINFOADBA: TT Row ID=4 
....
2024-03-22 11:05:06.382 D/DBINFOADBA: TT Row ID=10 a_column=INSRT_7 at 1711065515331
2024-03-22 11:05:06.382 D/DBINFOADBA: TT Row ID=11 a_column=INSRT_8 at 1711065515333
2024-03-22 11:05:06.382 D/DBINFOADBA: TT Row ID=12 a_column=INSRT_9 at 1711065515335
2024-03-22 11:05:06.382 D/DBINFOADBA: TT Row ID=13 a_column=INSRT_10 at 1711065515337


2024-03-22 11:05:06.415 D/DBINFOAINSRT: TT Row ID=1 a_column=PPDB ROW 1
2024-03-22 11:05:06.415 D/DBINFOAINSRT: TT Row ID=2 a_column=PPDB ROW 2
2024-03-22 11:05:06.415 D/DBINFOAINSRT: TT Row ID=3 a_column=PPDB ROW 3
....
2024-03-22 11:05:06.416 D/DBINFOAINSRT: TT Row ID=10 a_column=INSRT_7 at 1711065515331
2024-03-22 11:05:06.416 D/DBINFOAINSRT: TT Row ID=11 a_column=INSRT_8 at 1711065515333
....
2024-03-22 11:05:06.416 D/DBINFOAINSRT: TT Row ID=23 a_column=INSRT_10 at 1711065906412

As can be seen

  • the database existed. The prepackaged database callback was not invoked so the existing database was utilised. And accessing the database itself shops the 13 rows in existence (original 3 in the prepackaged database plus the 10 inserted during run 1)
  • the onOpen callback again confirms the 13 rows
  • the activity processing yet again confirms the 13 rows
  • after the inserts the expected 23 (original 13 + 10 inserted) rows exist.

In summary

Your provided code acts as is expected. the issue must be elsewhere.