Android SQLite Database Upgrade

Source: Internet
Author: User

 Public classDatahelperextendssqliteopenhelper{Private Static Final intVersion=1;  PublicDatahelper (Context context, String name, Cursorfactory factory,intversion) {        Super(context, name, Factory, version);//version represents the database version number//TODO auto-generated Constructor stub} @Override Public voidonCreate (Sqlitedatabase db) {//TODO auto-generated Method Stub} @Override Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) {        //TODO auto-generated Method Stub            }}

The Nupgrade method is called when the database needs to be upgraded. Can be used to delete tables or any other actions. If you want to add a new column you can use ALTER TABLE to insert the table. If you need to rename or delete a column, you can use ALTER TABLE to rename the old table, create a new table, and add the old table data to the new table. If an exception is thrown, it is automatically rolled back.

1. What does "Database Upgrade" mean in the Help document?

You have developed a program that is currently in version 1.0. The program uses the database. By the 1.1 version, you added a field to a table in the database. Then the software 1.0 version of the database in the software version 1.1 will be upgraded.

2. What should I pay attention to in database upgrade?

When the 1.0 version of the software is upgraded to version 1.1, the old data cannot be lost. Then in the 1.1 version of the program will be able to detect the new version of the software is incompatible with the old database, and there is a way to upgrade the 1.0 software database to 1.1 software can use the database. In other words, add that field to the table in the database of the 1.0 software and give the field the default value.

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

The constructor for the Sqliteopenhelper class has a parameter of int version, which means the database version number. For example, in software version 1.0, when we use Sqliteopenhelper to access the database, this parameter is 1, then the database version number 1 will be written in our database.

By the 1.1 version, our database needs to change, then our 1.1 version of the program will use an integer greater than 1 to construct the Sqliteopenhelper class, to access the new database, such as 2.

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

4. When will the database upgrade be triggered? How do I upgrade?

When the system constructs an object of the Sqliteopenhelper class, if the version number is found to be different, the Onupgrade function is automatically called, allowing you to upgrade the database here. Based on the above scenario, add fields to the corresponding tables of the old database in this function and add the default values to each record.

Both the new version number and the old version number are passed in as parameters to the Onupgrade function, making it easy for developers to know which version the database should be upgraded from.

When the upgrade is complete, the database automatically stores the latest version number for the current database version number.

Android SQLite Database Upgrade

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.