Problems with Android database updates

Source: Internet
Author: User

When updating the APK, if there is a DB structure or a preset value in it, you cannot replace the existing dB with the DB in the new version during APK version update installation and upgrade.
In addition, when you use APK, some data will be written into the DB. If you delete the existing dB and then import it by clearing the data in settings after the upgrade and installation,
Although new databases can be imported, user data will be deleted. It remains unchanged during use.
At the beginning of the design, the program did not take into account the user's need to modify the database. When there is a DB update, the user is allowed to clear the data.
The Database Import method is to copy the data of the entire database in assets, rather than dynamically creating the data.

Solution:
In onupgrade, you can back up the content added to an existing database. Then, you can use the file operation to delete the entire dB file, copy the new file to the DB path, and write the previously backed up content.
Two problems occurred during the modification.
1. In onupgrade, select cannot be performed, prompting that the data table is locked.
The db. query and DB. rawquery methods are used. Later, we can see that it is because DB = This. getwritabledatabase ();
Getreadabledatabase () should be used (). I don't know if it is feasible.
It may also be because the DB object in onupgrade is not used, or the SDK version is incorrect ??? I just don't have time to try again .....
In onupgrade, you only need to use db.exe csql to add a row to the existing dB. Then, when the program starts, read the database for determination,
If this row exists, the database is updated and then processed accordingly.
DB updated successfully.
However, I encountered the second problem:
2. If the DB version of the new version is later than the old version, the onupgrade method can be executed normally,
However, if you need to replace the old version in special circumstances, exception will occur,
Android. database. SQLite. sqliteexception: Can't downgrade database from version 2 to 1
Later, I found that the ondowngrade method was provided in level 11. Switch to SDK 4.03 and see this method.
At this point, all the problems are solved, but they are not perfect. Wait for time to modify them.

 

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.