Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Migrate from Sqlite to Realm (Android)

My existing app data is on SQlite. I am trying to migrate the data from Sqlite to Realm. I google how to migrate data, but didn't find any solution related to that.

I am planning to launch an update version of the existing app. While updating the app the data must migrate to the Realm and the existing Sqlite Database must be dropped. Please share some idea with solution if possible.

like image 641
Ncit Cosmos Avatar asked Jan 04 '17 05:01

Ncit Cosmos


1 Answers

Let's suppose you have TODO list in DataBase(SqlLite) stored and you want to migrate it to realm DB.

SqlLite interface for the TODO Item table

  interface TodoItemModel {
     String CREATE_TABLE = ""
            + "CREATE TABLE todo_list(\n"
            + "    _id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,\n"
            + "    name TEXT NOT NULL,\n"
            + ")";
    String TABLE_NAME = "todo_item";

    //columns
    String _ID = "_id";
    String NAME = "name";
}

Realm Object for the ToDo Item

 public class TodoItem extends RealmObject {

    @PrimaryKey
    private Long _id;
    private String name;

    public TodoItem() {
    }

    public TodoItem(long id, String name) {
        this._id = id;
        this.name = name;
    }
}

Step 1. Increment the version of the DB where you are extending SQLiteOpenHelper (ie: DbOpenHelper class given below).

Step 2. In DbOpenHelper class using onUpgrade function you can check the version and store all your SqlLite Data records to the realm db.

   final class DbOpenHelper extends SQLiteOpenHelper {

    //private static final int PREVIOUS_VERSION = 1;
    private static final int CURRENT_VERSION = 2;

    public DbOpenHelper(Context context) {
        super(context, "todo.db", null /* factory */, CURRENT_VERSION);
    }


    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(TodoItemModel.CREATE_TABLE);
    }


    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

        if (oldVersion < CURRENT_VERSION) {
            //get all the stored data in your db
            List<TodoItem> list = select_all_items_for_list(db);

            //open the realm transaction and store the list of todo's
            Realm realm = Realm.getDefaultInstance();
            realm.beginTransaction();
            realm.insertOrUpdate(list);
            realm.commitTransaction();
            //finally drop table
            db.execSQL("DROP TABLE IF EXISTS " + TodoItemModel.TABLE_NAME);
        }
    }

    private List<TodoItem> select_all_items_for_list(SQLiteDatabase db) {
        List<TodoItem> list = new ArrayList<>();
        Cursor cursor = db.rawQuery("select * from " + TodoItemModel.TABLE_NAME, new String[0]);

        if (cursor == null)
            return list;

        for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
            final TodoItem todoItem = new TodoItem(cursor.getLong(cursor.getColumnIndex(TodoItemModel._ID)), cursor.getString(cursor.getColumnIndex(TodoItemModel.NAME));
            list.add(todoItem);
        }
        cursor.close();

        return list;
    }
}
like image 193
Dharmendra Pratap Singh Avatar answered Oct 13 '22 17:10

Dharmendra Pratap Singh