Android Database Upgrade instance and android database instance

Source: Internet
Author: User

Android Database Upgrade instance and android database instance

Part 1

The SQLiteOpenHelper class of Andoird has an onUpgrade method. This method is triggered when the database is upgraded. After practice, I solved a series of questions:

1. What does "Database Upgrade" refer to in the help document?

You have developed a program. The current version is 1.0. The program uses the database. By 1.1, you have added a field to a table in the database. The database used for Software Version 1.0 will be upgraded in Software Version 1.1.

2. What should I pay attention to when upgrading the database?

Old data cannot be lost when the software version 1.0 is upgraded to version 1.1. In this case, the new software version is not compatible with the old database, in addition, it is possible to upgrade the database of 1.0 software to the database that 1.1 software can use. In other words, add the field to the table in the database of software 1.0 and assign the default value to the field.

3. How does the program know that the database needs to be upgraded?

The constructor of the SQLiteOpenHelper class has an int version parameter, which indicates the database version number. For example, in software 1.0, when we use SQLiteOpenHelper to access the database, this parameter is 1, and database version 1 will be written in our database.

In Version 1.1, our database needs to change. In our 1.1 program, we need to use an integer greater than 1 to construct the SQLiteOpenHelper class for accessing new databases, such as 2.

When our new program 1.1 reads the old database of version 1.0, it finds that the database version stored in the old database is 1, and the version number entered when our new program accesses it is 2, the system knows that the database needs to be upgraded.

4. When will the Database Upgrade be triggered? How to upgrade?

When the system constructs an object of the SQLiteOpenHelper class, if the version number is different, the onUpgrade function is automatically called to upgrade the database. Based on the above scenario, add fields to the corresponding tables of the old database version in this function and add the default values for each record.

Both the new and old versions are passed in as parameters of the onUpgrade function, so that developers can know which version the database should be upgraded.

After the upgrade, the database automatically stores the latest version number as the current database version number.

For details, refer to StackOverFlow's question and answer on "storage location of database version in Database.

 

Part 2Android 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:
1 alter table subpartition add column Activation BLOB;
2 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.
Alter table subpartition rename to _ temp _ subpartition;

2. Create a new table
Create table subpartition (OrderId VARCHAR (32) Primary key, UserName VARCHAR (32) NOTNULL, ProductId VARCHAR (16) Not null );
  
3. Import Data
Insert into subpartition SELECT OrderId, "", ProductId FROM _ temp _ subpartition;
Or
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
Drop table _ temp _ subpartition;

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.


Help: questions about database instantiation for android

Private static final String DATABASE_NAME = "test. db ";
Private static final int DATABASE_VERSION = 1;

Public class DatabaseHelper extends SQLiteOpenHelper {
Public DatabaseHelper (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
}

In this way, you can define the parameters first. You can try again. I am also engaged in android development. If you have any questions, please contact me. My QQ account is 379371398 and I hope to adopt it.

Android connects to the SQL server database instance.

Search for www.csdn.net/

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.