When you see this blog post, first you need to understand oncreate this method of creation, and then continue to the following! (refer to my previous post http://www.cnblogs.com/896240130Master/p/6119616.html)
This onupgrade class should be built on the basis of the OnCreate class! We know that Onupgrade is meant to be upgraded.
Look at the code: Create a new class Sqltext.java
PackageCOM.EXAMPLE.SJK;ImportAndroid.content.Context;ImportAndroid.database.sqlite.SQLiteCursor;Importandroid.database.sqlite.SQLiteDatabase;Importandroid.database.sqlite.SQLiteDatabase.CursorFactory;ImportAndroid.database.sqlite.SQLiteOpenHelper;ImportAndroid.util.Log; Public classSQLTextextendssqliteopenhelper{Private Static FinalString db_name = "mydata.db";//Database name Private Static Final intVersion = 2;//The current database version number is upgraded to 2. PublicSQLText (Context context) {Super(Context, Db_name,NULL, version); } @Override Public voidOnCreate (Sqlitedatabase db) {//The newly installed software starts hereString sql_message = "CREATE TABLE t_message (ID int primary key,name varchar (), age varchar (50),sex varchar (TEN))"///Upgrade added a column of sex, so in order to upgrade with version 2 here also to correspond to Db.execsql (sql_message); LOG.I ("OnCreate", "You are a new user, we are helping you to create a table---> Success"); String Sql_up1= "INSERT into t_message values (1, ' small white ', ' 18 ', ' Male ')"; Db.execsql (SQL_UP1); LOG.I ("OnCreate", "You are the new user, we help you insert a data--success"); } @Override//updates on the original software will start here without uninstalling the online update Public voidOnupgrade (Sqlitedatabase db,intOldversion,intnewversion) { //This article discusses this method if(oldversion = = 1) {//if the version is 1.0, upgrade the content below or modifyString sql_upgrade = "ALTER TABLE t_message add sex varchar (10)";//Add a column of sexDb.execsql (Sql_upgrade); String sql_up2= "INSERT into t_message values (3, ' Little Red ', ' 18 ', ' Male ')"; Db.execsql (SQL_UP2); LOG.I ("Onupgrade", "You update version 2.0 online without uninstalling, and the list adds a column of sex"); } }}
Show in Mainactivity.java
Package Com.example.sjk;import Android.app.activity;import Android.database.cursor;import Android.database.sqlite.sqlitedatabase;import Android.os.bundle;import Android.widget.textview;public Class Mainactivity extends Activity {private SQLText st;//SQLText contextprivate TextView TV for this category; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); TV = (TextView) Findviewbyid (R.id.textview);//Find this TextView component St = new SQLText (mainactivity.this); String Text = query ();//Call the method Tv.settext ("ID \ T" + "name\t" + "age\t" + "Sex\ n "+text+" \ n ");//display} public string query () {//Query database data String result =" "; Get database object Sqlitedatabase db = St.getreadabledatabase ();//Read-only: readabledatabase; Read and write: Writabledatabase//Querying data in database cursor cursor = Db.query ("T_message", NULL, NULL, NULL, NULL, NULL, NULL);//result set for (int i=0;i<cursor.getcount (); i++) { Cursor.movetonext (); result + = Cursor.getint (Cursor.getcolumnindex ("id")); result + = Cursor.getstring ( Cursor.getcolumnindex ("name")); result + = Cursor.getstring (Cursor.getcolumnindex ("Age"));result + = Cursor.getstring (Cursor.getcolumnindex ("Sex"));}cursor.close ();//close result set db.close ();//Close database object return result; }}
Now we are on version number 1, install this new version 2. That is to say, do not uninstall version 1, directly overwrite version 2 see
The version was updated and a column was added
What we're inserting is a piece of data, why is it two, because we already have a piece of data in version 1.0, and the next sex at 1.0 doesn't, so it's null,
Let's try it again for the 2nd time, uninstall the installation. Reinstall to see the original data or to perform the OnCreate method or Onupgrade method?
Results:
Show:
One of our conclusions:
When you install a new app on Android, it will be created from the OnCreate method.
When your Android is updated on older versions, it will be updated from the Onupgrade method.
On the onupgrade example of Sqliteopenhelper