humazed / RoomAsset

A helper library to help using Room with existing pre-populated database [DEPRECATED].
Apache License 2.0
135 stars 23 forks source link

Mapping Pre-populated database with column type NUMERIC #15

Closed cac-william closed 6 years ago

cac-william commented 6 years ago

Hello,

I have a pre-populated database with a column type NUMERIC and it seems that I cannot find an appropriated java type to map it. Just wonder how do I define my entity class to map this column?

This is the create statement:

CREATE TABLE function01tree (
fun_order NUMERIC,
fun_type INTEGER,
fun_no INTEGER PRIMARY KEY,
fun_parent_no INTEGER,
fun_descript TEXT,
fun_level INTEGER)

Thanks for the great library

humazed commented 6 years ago

from this table it translates to java.math.BigDecimal

but it really depends on the data in your database. if all the data in this column are integers then use int, if they are double use double.

cac-william commented 6 years ago

Thank you for the reply.

Actually the data in the column are just integers. I tried to use Integer but I got

java.lang.IllegalStateException: Migration didn't properly handle function01tree.
 Expected:
TableInfo{name='function01tree',columns={
    fun_descript=Column{name='fun_descript', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0},
    fun_level=Column{name='fun_level', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0},
    fun_order=Column{name='fun_order', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, 
    fun_no=Column{name='fun_no', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, 
    fun_parent_no=Column{name='fun_parent_no', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, 
    fun_type=Column{name='fun_type', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, 
    foreignKeys=[], indices=[]}
  Found:
TableInfo{name='function01tree', columns={
    fun_descript=Column{name='fun_descript', type='TEXT', affinity='2', notNull=false, primaryKeyPosition=0}, 
    fun_level=Column{name='fun_level', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, 
    fun_order=Column{name='fun_order', type='NUMERIC', affinity='1', notNull=false, primaryKeyPosition=0}, 
    fun_no=Column{name='fun_no', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=1}, 
    fun_parent_no=Column{name='fun_parent_no', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}, 
    fun_type=Column{name='fun_type', type='INTEGER', affinity='3', notNull=false, primaryKeyPosition=0}}, 
    foreignKeys=[], indices=[]}

If I use BigDecimal, I got Cannot figure out how to save this field into database. You can consider adding a type converter for it. Then I face the same problem that which data type I should convert to/from.

humazed commented 6 years ago

you can use double, or use sqlite browser to change the datatype to integer.

cac-william commented 6 years ago

I use the sqlite browser to change the data type to integer then everything works.

Thank you for the support

humazed commented 6 years ago

You are welcome.