How to modify type in a table of SQLite in Android, using onUpgrade method? How to modify type in a table of SQLite in Android, using onUpgrade method? sqlite sqlite

How to modify type in a table of SQLite in Android, using onUpgrade method?


SQLite indeed does not allow changing existing table columns.

However, SQLite uses dynamic typing, so what column type you set in the table definition does not really matter (except for conversions due to type affinity).

In the general case, any changes to tables (other than new columns) require that you create a new table:

CREATE TABLE TempTable (...);INSERT INTO TempTable SELECT col1, col2, ... FROM MyTable;DROP TABLE MyTable;ALTER TABLE TempTable RENAME TO MyTable;

If you want to change the type of values already stored in the table, you have to use CAST:

...INSERT INTO TempTable SELECT CAST(col1 AS TEXT), ... From MyTable;...

For new values, just put a value with the correct type into the ContentValues object.