The "Android" database is based on the "incremental update" of the script, with no need to modify Java code for each update!

Source: Internet
Author: User

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 specific method to ask Niang) to do the database upgrade, the file name identifies the corresponding version, Java in accordance with "previous version, current version" Select the execution of the script.

When upgrading, modify the Db_version (current version).


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, execute script//Note:1> Execute "(OLDV,NEWV]" (oldv=0) between script//2> missing script will skip private voi directlyD initdb (sqlitedatabase db, int oldversion, int newversion) {for (int i = oldversion + 1; I <= newversion; i++) execdbsc Ript (db, "db/update" + i + ". sql");} Execute 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 ();}} The batch is executed 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 execution of drop directly over try {db.execsql (sqlstatement + ";"),} 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:





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.