Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – How to import data into a Room database upon creation?

Posted on November 14, 2022

Solution 1 :

There are basically two ways to import data into a Room database upon creation. The first uses an ugly workaround but then allows you to use Room Entities and all. The second is to work directly with the SQLiteDatabase instance provided.

1. Insert a guard and work with Room abstractions

    final boolean[] doImport = { false };

    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "my-db")
            .addCallback(new RoomDatabase.Callback() {
                @Override
                public void onCreate(@NonNull SupportSQLiteDatabase db) {
                    super.onCreate(db);
                    doImport[0] = true;
                }
            })
            .build();

    db.userDao().get(17);

    if (doImport[0]) {
        UserEntity user = new UserEntity();
        user.name = "John Doe";
        db.userDao().insert(user);
    }

The doImport boolean serves as a guard to protocol, whether the onCreate callback has been called. It needs to be an array though, because a new value couldn’t be assigned to a simple boolean from within onCreate.

Notice also the seemingly senseless line db.userDao().get(17);. It is necessary to access the database in order for the onCreate callback to be called. Otherwise doImport would remain false at this point, regardless of whether or not the database was newly created.

Finally in the last if block the database may be accessed with all the nice abstractions Room provides.

2. Work with the SQLiteDatabase directly

    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "my-db")
            .addCallback(new RoomDatabase.Callback() {
                @Override
                public void onCreate(@NonNull SupportSQLiteDatabase db) {
                    super.onCreate(db);

                    db.beginTransaction();
                    ContentValues values = new ContentValues();
                    values.put("name", "John Doe");
                    db.insert("UserEntity", SQLiteDatabase.CONFLICT_ABORT, values);
                    db.setTransactionSuccessful();
                    db.endTransaction();
                }
            })
            .build();

A lot less painful then I suspected. You need to work with strings though to identify database tables and fields, which is error prone!

Problem :

I’ve upgraded my app to feature a Room database. When the user upgrades to my new app version, I need to import data into the Room Database that was stored in other data structures. At first glance it looks like Room already supports this scenario:

    db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "my-db")
            .addCallback(new RoomDatabase.Callback() {

                @Override
                public void onCreate(@NonNull SupportSQLiteDatabase db) {
                    super.onCreate(db);

                    // IMPORT DATA
                }
            })
            .build();

However, when trying to actually import data, things get more complicated. When I try to import data using Entities, Daos and the AppDatabase, I run into an Exception:

java.lang.IllegalStateException: getDatabase called recursively

It doesn’t seem to be possible to import data into the database using all the nice Room Entities, Daos and so forth. The onCreate method does however provide access to the underlying SQLite database. Maybe I’m meant to import data there?

READ  [ANSWERED] Manually Storing images in Firebase Storage from browser .How to make it reflect in real time database. so that I can fetch in Android app
Powered by Inline Related Posts

The documentation on using the SQLite database directly is rather thin. And it starts with a big red warning not to access the SQLite database directly but to use the Room abstractions instead!

How should I proceed? How is this usually done?

Do the tables I’ve defined with Entities already exist at this point? Or do I need to create the tables with SQL statements before I can start to import my data?

Comments

Comment posted by prepopulate your Room DB

Have you considered using your existing DB to

Comment posted by user1785730

There is no existing database in my app.

Comment posted by Bob Snyder

What are the “other data structures” that contain the existing data?

Comment posted by stackoverflow.com/q/50775527/4815718

See the answers to this question:

Comment posted by developer.android.com/reference/android/arch/persistence/db/…

As you have a

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress