Method of database upgrading and adding new columns to tables in Android Development
This example describes the method of database upgrading and adding new columns to tables in Android development. Share with you for your reference, as follows:
Today, it suddenly occurred to us that we often encounter problems when upgrading the Android version. The database may be modified in the new version. Today, we will write a test program with the example of database upgrading and adding new columns to the table.
First, to create a database, we usually create a dbhelper first, inherit sqliteopenhelper, and use the constructor passing the version number:
We initialize dbhelper in activity and pass in the version number of the current application,
We know by looking at the sqliteopenhelp source code that when we call
Called when
Method. Looking at the following, we can see that the onupgrade method will be called when the version number is greater than the current one. We can upgrade the database here:
Therefore, in the onupgrade method, I did the following operations to add new columns to the table:
In this way, there will be an additional column of talknumber after execution,
This completes the database upgrade when the version upgrade is completed, and adds a new column to the table.
Demo code in: https://github.com/xxnan/DataBaseUpdateTest
Or click here to download.
More readers interested in Android related content can view the special topics of this site: summary of Android database operation skills, summary of Android SQLite database operation skills, introduction and advanced tutorial of Android development, summary of Android resource operation skills, summary of Android view skills and summary of Android control usage
I hope this article will help you in Android programming.