5

I have a SQLite DB which I cannot change or migrate and need to map a NUMERIC column to my Kotlin class using Room. What do I need to use as a data type of the field in Kotlin class? When using Boolean, Integer, Double, String, it fails with similar error message:

Expected:
TableInfo{name='calendars', columns={monday=Column{name='monday', type='REAL', affinity='4', ...}
 Found:
TableInfo{name='calendars', columns={monday=Column{name='monday', type='NUMERIC', affinity='

EDIT: I suppose it's possible to update the database and change column type. But I still want to know if there is a way to map NUMERIC with Room.

Jakub Turcovsky
  • 2,096
  • 4
  • 30
  • 41
  • Have you tried BigDecimal? – william xyz May 19 '19 at 18:33
  • @williamxyz Yes, but it doesn't compile and wants me to add a converter. But I can't do a converter, if I can't map the field to anything. – Jakub Turcovsky May 19 '19 at 18:35
  • 1
    *I have a SQLite DB which I cannot change or migrate*. You can change (non-destructively), an example of doing so (changing boolean or byte to INTEGER) is here [Can't migrate a table to Room do to an error with the way booleans are saved in Sqlite](https://stackoverflow.com/questions/56193992/cant-migrate-a-table-to-room-do-to-an-error-with-the-way-booleans-are-saved-in/56194741#56194741) – MikeT May 19 '19 at 20:59

1 Answers1

3

If you have an existing database, open it with SQLite Browser, change the datatype there by selecting the table and editing it (changing the type to REAL). This preserves the data and afterwards you can use the database again.

Currently, Room seems to have problems mapping these datatypes, so you have to explicitly use REAL in your database.

Also it might be a good idea to annotate your field in your data object respectively.

@ColumnInfo(typeAffinity = ColumnInfo.REAL)
public double yourField;
Alexander Pacha
  • 9,187
  • 3
  • 68
  • 108