Android Room change type of column with Migration

試著忘記壹切 提交于 2020-06-17 14:38:05

问题


I didn't find any answer to what I'm searching for so I'm sending this question.

I am using Room for Android.

I have an Entity with an Int column and I need to change it to Double, and I don't know how to do it.

Does anybody know how to do it?

My question might be dumb, but I didn't find any answer on stackoverflow/any google search.


回答1:


You need to create new table with the new schema. Copy data from old table to the new one. Drop old table. Rename new table to the name of old table.

Here you've got nice article about that: https://medium.com/androiddevelopers/understanding-migrations-with-room-f01e04b07929




回答2:


As mentioned in documentation for sqlite over here, updating column type is not supported in sqlite.

SQLite supports a limited subset of ALTER TABLE. The ALTER TABLE command in SQLite allows the user to rename a table or to add a new column to an existing table. It is not possible to rename a colum, remove a column, or add or remove constraints from a table.

As pointed by Sebastian M you will need to create new table with the new column added, Copy data from old table to the new one. Drop old table. Rename new table to the name of old table.



来源:https://stackoverflow.com/questions/53668302/android-room-change-type-of-column-with-migration

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!