modifying table definitions
The SQLite number Library has very limited support for ALTER TABLE commands, only adding columns at the end of the table, cannot modify the column definition, and cannot delete existing columns. So what if you want to modify the table? We can use the temporary table approach. Specifically, there are four steps:
Rename an existing table to a temporary table;
Create a new table;
Import data from the staging table into a new table (Note that the modified column is processed);
Deletes a temporary table.
Take the example of V2 upgrade to V3 as an example:
public void Onupgrade (sqlitedatabase db, int oldversion, int newversion) { if (oldversion==2) { Db.execsql ("AlterTABLE t_region RENAMETo T_region_temp"); Db.execsql ("CREATETABLE T_region (_idIntegerPrimaryKey" + "AutoIncrement, Regionvarchar, codevarchar " +" Country varchar) Db.execsql ("insert into t_region (_id, Region, code, country) " +" select _id, Region, code, \ "china\" from t _region_temp "); Db.execsql ("drop table t_region_temp }} /span>
Upgrade the database version---preserve the original data when you upgrade the app