kotlinandroid-roomandroid-room-prepackageddatabase

What would be the equivalent of a datetime bigint column in roomdb


I have a pre-existing sqlite database, the date time is saved as ticks so the sqlite column is bigint. How would I create a roomdb entity for that?

I tried using Long in RoomDb but it shows as INTEGER when I generated a database, Also tried using a converter to Long to DateTime but it still store as integer.


Solution

  • I tried using Long in RoomDb but it shows as INTEGER when I generated a database, Also tried using a converter to Long to DateTime but it still store as integer.

    That is correct Long (kotlin) would be stored in a column with an SQLite type of INTEGER, which can hold a 63 bit signed integer value.

    Although in SQLite the column type is highly flexible (in most situations any type of value can be stored in any type of column) Room requires a much stricter column type limited to

    A TypeConverter is only required if a field in an @Entity annotated class is not catetered for directly (Room caters for the above) such as other objects (e.g. Date).

    If you have a pre-existing database then it may well have to be converted to suit Room's expectattions.

    You mention BIGINT as a type. Room will not accept this. The column type would have to be changed to INTEGER. The conversion of the column types (and importantly constraints such as NOT NULL) MUST match Room's expectations.

    The simplest way to ascertain Room's expectations is to

    1. define the @Entity annotated classes.
    2. Include them in the @Database's entities parameter.
    3. Successfully compile the project (CTRL+F9)
    4. Via the Android View find the java(generated) directory.
    5. Within the directory find the file/class that is the same name as the @Database annotated class.
    6. Within the class locate the createAllTables function, which contains the SQL Room would use to create the tables i.e. the tables EXACTLY as Room expects.

    The database may, certainly in the case of using BIGINT as a column type, need to be converted. This could be prior to including the database file as an asset, or could be via the prepackageddatabase callback.