Ideas:
1. Of course is based on sqliteopenhelper.oncreate (called when the program is first installed), OnUpdate (called when the program is upgraded)
2. Use "Script" (Scripting detailed method to ask Niang) to do the database upgrade, the file name identifies the corresponding version number, Java in accordance with the "previous version number, the current version number" select Run script.
When upgrading, change Db_version (the current version number) is available.
Dbmanager.java:
Package Com.example.test;import Java.io.bytearrayoutputstream;import Java.io.ioexception;import Java.io.inputstream;import Android.content.context;import Android.database.sqlite.sqlitedatabase;import Android.database.sqlite.sqliteopenhelper;public class DBManager extends Sqliteopenhelper {private final static String DBNAME = "Mydata.db";p rivate final static int db_version = 1;private static DBManager instance;public DBManager (Context Co ntext) {//TODO auto-generated Constructor Stubsuper (context, DBNAME, NULL, db_version);} public static DBManager Getthis () {if (instance = = null) instance = new DBManager (Mainactivity.getthis ()); return instance; } @Overridepublic void OnCreate (Sqlitedatabase db) {//TODO auto-generated Method Stubinitdb (db, 0, db_version);} @Overridepublic void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {//TODO auto-generated method stubinitd B (DB, oldversion, newversion);} Initialize DB, run script//Note:1> run "(OLDV,NEWV]" (New Installation, oldv=0) script between//2> missing script will skip directly to private VoiD initdb (sqlitedatabase db, int oldversion, int newversion) {for (int i = oldversion + 1; I <= newversion; i++) execdbsc Ript (db, "db/update" + i + ". sql");} Run the script private void Execdbscript (Sqlitedatabase db, String assetname) {try {InputStream is = Mainactivity.getthis (). Getas Sets (). open (Assetname); String stats = read (is); Execsqls (db, stats);} catch (Exception e) {//Todo:handle exceptione.printstacktrace ();}} Batch run, with ";" Separate private void Execsqls (Sqlitedatabase db, String stats) {//Oncreate/onupdate internal with transaction, multilayer nesting no impact (outermost function) Db.begintransaction (); string[] Sqls = Stats.split (";"); for (int i = 0; i < sqls.length; i++) {String sqlstatement = Sqls[i].trim ();//TODO you could want to parse out comments HEREif (sqlstatement.length () > 0) {//Catch error, prevent direct crossing of try {db.execsql (sqlstatement + ";") when running Drop,} catch (Exception e) {// Todo:handle exceptione.printstacktrace ();}}} Db.settransactionsuccessful ();d b.endtransaction ();} public static String Read (InputStream instream) throws IOException {ByteArrayOutputStream BOS = new Bytearrayoutputstream (); byte[] buffer = new Byte[1024];int len = 0;while (len = instream.read (Buff ER)) =-1) {bos.write (buffer, 0, Len);} string ret = new string (Bos.tobytearray ()); return ret;}}
DB Script:
The "Android" database is based on the " incremental update ", and the Java code is not required for each update!