This article describes the method of upgrading databases and adding new columns to tables in Android development. Share it for your reference, as follows:
Today I suddenly thought that when we upgrade the Android version, we often encounter the upgraded version. The database may be modified in the new version. Today, we will write a test program using the database upgrade and the table is added as an example.
First, when we want to create a database, we generally create a DbHelper, inherit SQLiteOpenHelper, and we use the constructor to pass the version number:
public DbHelper(Context context, String name, int version){ this(context, name, null, version); }
We initialize DbHelper in the Activity to pass the version number of the current application.
try { ver=getVersionName(getApplicationContext()); } catch (Exception e) { (); } myDbHelper = new DbHelper(this,DbHelper.DB_NAME,ver); db= ();
We know by looking at the SQLiteOpenHelpe source code, when we call
getWritableDatabase()
When the
getDatabaseLocked(boolean writable)
Method, look below to see that when the version number is greater than the current one, the onUpgrade method will be called. We can do the upgrade database operation here:
final int version = (); if (version != mNewVersion) { if (()) { throw new SQLiteException("Can't upgrade read-only database from version " + () + " to " + mNewVersion + ": " + mName); } (); try { if (version == 0) { onCreate(db); } else { if (version > mNewVersion) { onDowngrade(db, version, mNewVersion); } else { onUpgrade(db, version, mNewVersion); } } (mNewVersion); (); } finally { (); }
Therefore, I did the table adding new column operation in the onUpgrade method as follows:
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { if(newVersion>oldVersion) { ("ALTER TABLE local_picc_talk ADD talknumber varchar(20);"); } }
This way, after execution, there will be an additional column of talknumber.
select * from sqlite_master where type="table" and name="local_picc_talk"; table|local_picc_talk|local_picc_talk|9|CREATE TABLE local_picc_talk (id integer PRIMARY KEY autoincrement,case_no varchar(60),user_no varchar(60),user_name var char(60),car_no varchar(60),task_work_id varchar(60),talk_content varchar(1000), time varchar(60),with_talk_no varchar(60),with_talk_name varchar(60), talknumber varchar(20), UNIQUE (id)) sqlite> select * from local_picc_talk;
This completes the database upgrade during the version upgrade and adds a new column to the table.
The DEMO code is in:/xxnan/DataBaseUpdateTest
Or click hereDownload this site。
For more information about Android related content, please check out the topic of this site:Android database operation skills summary》、《Summary of Android's SQLite database skills》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.