Migrating from Sqlite to Realm (Android)

My existing application data is on SQlite. I am trying to transfer data from Sqlite to Realm. I google how to transfer data, but did not find solutions related to this.

I plan to launch an updated version of an existing application. When updating the application, data must migrate to the Kingdom, and the existing Sqlite database must be deleted. If possible, share the idea with the solution.

+4
source share
1 answer

Suppose you have a TODO list in a DataBase (SqlLite) and you want to transfer it to the database.

SqlLite Interface for TODO Position 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 ToDo object

 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;
    }
}

1. , SQLiteOpenHelper ( : DbOpenHelper, ).

2. DbOpenHelper onUpgrade SqlLite 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;
    }
}
+7

Source: https://habr.com/ru/post/1665679/


All Articles