Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use pre-populated databases with Realm

How could we convert SQLite database to a Realm database?

Is there any way to use pre-populated databases with Realm on Android?

like image 962
nekofar Avatar asked Oct 07 '14 15:10

nekofar


People also ask

What database does Realm use?

In a current GlobalLogic project, we are managing a 3MB database in SQLite running on Android.

Does Realm use SQLite?

Realm is an easy-to-use, open-source alternative to SQLite. The key difference between the two is that Realm is an object database management system, while SQLite is a relational database management system. Realm is often used to replace SQLite.

Does Realm store data locally?

Realm is an offline first database - meaning that data is always stored locally first and then sync'd to the cloud. If the user disconnects from their account, their data is still on the device and not lost.

Is Realm a noSQL database?

Realm is essentially a noSQL database which means that with Realm, you can store and retrieve data that is modeled in means other than tabular relations of relational databases. This makes for a much more familiar data structure that is easier to maintain.


2 Answers

Currently there is no way to automatically convert a SQLite database to a Realm database, you would have to manually read all data from the SQLite database and insert them into Realm.

An alternative could be the Realm browser that might make this easier, but it is currently available for MacOS X only. You can read more here: https://github.com/realm/realm-java/issues/435

For the second part: As Realm databases are just a single file so you can easily add a pre-populated realm database to you app and reference it with Realm.getInstance().

like image 126
Christian Melchior Avatar answered Sep 28 '22 02:09

Christian Melchior


This is an answer to the second part of the question.

While shipping a prepopulated database in the assets folder of your app, analogous to the official Objective-C to bundle of Realm with your app, is still probably the fastest way to get a prepopulated Realm if you have a lot of data. But as of Realm Java 0.89 there is now an official way to prepopulate a Realm database on Android.

There is now a method that allows for specifying a transaction to be run when a Realm database is created for the first time. You should call this method, initialData(Realm.Transaction transaction), as part of setting up the RealmConfiguration Builder.

For example

RealmConfiguration config = new RealmConfiguration.Builder(context)
  .name("myrealm.realm")
  .initialData(new MyInitialDataRealmTransaction()), 
  .build();
like image 21
Jade Avatar answered Sep 28 '22 03:09

Jade