Based on Android SQLite upgrade detailed _android

Source: Internet
Author: User
Tags sqlite sqlite database

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.