Write a class that inherits from Sqliteopenhelper
The system will automatically add the construction method, the OnCreate method, the Onupgrade method
We need to upgrade the database when the data or table structure in the database is changed.
This time, version plus 1. Make the corresponding changes in the update.
It is important to note that if you need to test the update, each time you start the test, the version value increases, and if the same as the last time, the Update method will not be sent
Put the code below.
First, the original table structure, the corresponding code
Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;
public class Datahelp extends Sqliteopenhelper {
Public static String name = "SXJJ.DB3";
Public static int version = 1;
Public Datahelp (Context context, String name, Cursorfactory factory,
int version) {
Super (context, name, null, version);
}
public void OnCreate (Sqlitedatabase db) {
String sql_tansinfo = "CREATE table if not exists transinfo (ID integer primary key autoincrement, name nvarchar (), tel N varchar (), content text, type int) ";
Db.execsql (Sql_tansinfo);
}
public void Onupgrade (sqlitedatabase arg0, int arg1, int arg2) {
}
}
Now the table structure, the corresponding code
Import com.ylj.sxbmzx.view.QueryTransInfoActivity;
Import Android.content.Context;
Import Android.database.sqlite.SQLiteDatabase;
Import Android.database.sqlite.SQLiteOpenHelper;
Import Android.database.sqlite.SQLiteDatabase.CursorFactory;
Import Android.widget.Toast;
public class Datahelp extends Sqliteopenhelper {
Public static String name = "SXJJ.DB3";
Public static int version = 3;
Context Context=null;
Public Datahelp (Context context, String name, Cursorfactory factory,
int version) {
Super (context, name, null, version);
This.context=context;
}
public void OnCreate (Sqlitedatabase db) {
// String sql_tansinfo = "CREATE table if not exists transinfo (ID integer primary key autoincrement, name nvarchar (), tel N varchar (a), content Text,type int,address text,marker text) ";
String sql_tansinfo = "CREATE table if not exists transinfo (ID integer primary key autoincrement, name nvarchar (), tel N varchar (a), content Text,type int,address text,marker text) ";
Db.execsql (Sql_tansinfo);
}
public void Onupgrade (sqlitedatabase db, int arg1, int arg2) {
First run, version plus 1, trigger this method, the next time will not trigger
// String sql_tansinfo = "CREATE table if not exists transInfo1 (ID integer primary key autoincrement, name nvarchar (), tel nvarchar (), content text,marker text, type int,address text) ";
// Db.execsql (Sql_tansinfo);
// String Sql_tansinfo_insert = "INSERT into TransInfo1 (ID integer primary key autoincrement, name nvarchar (), tel nvarchar (a), content text,marker text, type int,address text) ";
// String sql_delete= "Delete from Transinfo";
// Db.execsql (Sql_delete);
// String sql= "ALTER TABLE transinfo ADD COLUMN address text;";
// Db.execsql (SQL);
//
// String sql2= "ALTER TABLE transinfo ADD COLUMN marker text;";
// Db.execsql (SQL2);
Toast.maketext (Context, "Update---------", 1). Show ();
It is calculated that alter TABLE is really executed, and the address found is null
Now I don't know if the original data is still retained after ALTER TABLE, but the new field is empty, no data
After changing the procedure, check the time, it is necessary to find out the new fields, so, re-insert the data bar
Now the table is actually to add two columns, then I have to execute two times Insert column, can also delete the original table, re-build the table
Add two columns of this, later versions are added, nor appropriate
String sql_create_tansinfo = "CREATE table if not exists transinfo_new (ID integer primary key autoincrement, name nvarchar (a), tel nvarchar, content text,type int,address text,marker text) ";
No inserts.
Db.execsql (Sql_create_tansinfo);
String sql_drop_oldtable= "drop table Transinfo";
Db.execsql (sql_drop_oldtable);
String sql_rename= "ALTER TABLE transinfo_new Rename to Transinfo";
Db.execsql (Sql_rename);
}
}