Android applications may inevitably deal with SQLite. With the continuous upgrading of applications, the original database structure may no longer adapt to new features. At this time, the structure of the SQLite database needs to be upgraded.
SQLite provides the alter table command, allowing you to rename or add new fields to an existing TABLE, but cannot delete fields from the TABLE.
You can only add fields at the end of the table. For example, you can add two fields to subpartition:
Copy codeThe Code is as follows: alter table subpartition add column Activation BLOB;
Alter table subpartition add column Key BLOB;
In addition, if you encounter complex modification operations, such as data transfer while modifying, You can execute the following statement in a transaction to modify the table.
1. Change the table name to a temporary table.
Copy codeThe Code is as follows: alter table subpartition rename to _ temp _ subpartition;
2. Create a new table
Copy codeThe Code is as follows: create table subpartition (OrderId VARCHAR (32) primary key, UserName VARCHAR (32) not null, ProductId VARCHAR (16) not null );
3. Import Data
Copy codeThe Code is as follows: insert into subpartition SELECT OrderId, "", ProductId FROM _ temp _ subpartition;
OrCopy codeThe Code is as follows: insert into subpartition () SELECT OrderId, "", ProductId FROM _ temp _ subpartition;
* Note that double quotation marks (") are used to supplement data that does not exist.
4. delete a temporary table
Copy codeThe Code is as follows: drop table _ temp _ subtasks;
The above four steps can be used to migrate the old database structure to the new database structure, and ensure that the data will not be lost due to the upgrade.
Of course, you can also create a temporary table to reduce fields.