Android apps will inevitably deal with SQLite. With the continuous upgrade of the application, the original database structure may no longer adapt to the new features, this time, you need to upgrade the structure of the SQLite database.
SQLite provides ALTER TABLE commands that allow users to rename or add new fields to existing tables, but they cannot delete fields from the table.
And you can only add fields to the end of the table, for example, add two fields to subscription:
Copy Code code as follows:
ALTER TABLE Subscription ADD COLUMN activation BLOB;
ALTER TABLE Subscription ADD COLUMN Key BLOB;
In addition, if you encounter complex modification operations, such as the need for data transfer while modifying, you can implement the following statement in a transaction to implement the need to modify the table.
1. Change the table name to a temporary table
Copy Code code as follows:
ALTER TABLE Subscription RENAME to __temp__subscription;
2. Create a new table
Copy Code code as follows:
CREATE TABLE Subscription (OrderId VARCHAR PRIMARY KEY, UserName VARCHAR () not NULL, ProductId VARCHAR () NOT NULL) ;
3. Import data
Copy Code code as follows:
INSERT into Subscription SELECT OrderId, "", ProductId from __temp__subscription;
Or
Copy Code code as follows:
INSERT into Subscription () SELECT OrderId, "", ProductId from __temp__subscription;
* Note that double quotes "" is used to supplement data that doesn't exist.
4. Delete temporary tables
Copy Code code as follows:
DROP TABLE __temp__subscription;
Through the above four steps, you can complete the migration of the old database structure to the new database structure, and it can guarantee that the data should not be lost for the upgrade.
Of course, you can do this by creating a temporary table if you are experiencing a decrease in the field.