Android Database Seamless Upgrade Solution

Source: Internet
Author: User

During the software iteration, the business is constantly updated and the software is continually updated. Accordingly, database update upgrade is also an inevitable link. With Android as a client app, database upgrades can be a bit trickier for the server. Common ways to upgrade are:

1. Delete the old table and data and create a new table. The advantage is simple and convenient, the disadvantage is that the old data is lost. For applications where data dependency is low.

2. Work with each version of the database in your code, create a new table, and migrate the old data to the new table. The advantage is that the old data is retained, and the disadvantage is the need to deal with compatible versions of the database differences, more cumbersome. If you record maintenance version differences through code, you can cause your code to be bloated and extremely error-prone.

This article introduces a simple and seamless database upgrade scheme, which belongs to the second method mentioned above, but it is simple and efficient to deal with database version compatibility. The code has been implemented in Dbframework, a lightweight database framework that is simple, prescriptive, efficient, and can handle the inheritance relationships between tables, saving you a lot of development work.

Interested friends can understand.

A database upgrade is actually an upgrade of the tables in it. To update the table and preserve the data, you must know the difference between the new table and the old table. It is a difficult and complicated task if we rely on developers to record the differences between versions of the tables. So when we are in the database, does the client application have any way of knowing the differences and knowing which tables need to be updated? The answer is yes. In SQLite, there is a system table called Sqlite_master, which records all the table information in SQLite, as follows:

You can see that the table name is processed, as well as the table creation statement. With the CREATE statement, we can parse out the field information of the table, then compare the fields of the new table and the old table, the difference is that the table needs to be updated, and vice versa. What's even simpler is that we create a new table directly with no scolding, and then directly compare the new table and the old table's creation statement (ignoring the table name) to see if there are any differences to decide whether to update the table. Then perform specific update operations, migrate data, delete useless tables-old tables, or new tables created with aliases but not updated.

Android Database Seamless Upgrade Solution

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.