0

I am using Room to manage SQLite databasae in my app. The thing is that I am migrating a project, in which I download some data from a private webservice and finally I am receiving a ContentValues object which contains all the data to save to my POJO (let's say UserPojo). Because I have this ContentValues object, I initially thought in using the SqLiteOpenHelper's insert method, but I do not know how to get an instance of it from my RoomDatabase, neither whether it is a good practice to implement, neither how to alternatively do it...

What I tried was the following:

MyDatabase.getDatabaseInstance(MyApplication.getAppContext()).getOpenHelper().getWritableDatabase().insert(currentTable, CONFLICT_NONE, rowValues);

Is it okey? Where should I call it from? ViewModel? A new DAO? How shall I do it? Thanks.

mantc_sdr
  • 451
  • 3
  • 17

1 Answers1

0

If you are inserting into Room, then you would have an Entity which is really a normal class with annotations so that Room can create and access the table.

As such you could create an instance(s) of the class/entity from the ContentValues (using the appropriate get methods) and then use Room to insert(s) (@Insert).

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • the thing was to use a generic method, avoiding switching the entity, in other words, a method working for all tables just passing the tableName as parameter – mantc_sdr Apr 30 '21 at 08:28