The "Android" database is based on the " incremental update &quot, and the Java code is not required 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 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 &quot; incremental update &quot, and the Java code is not required for each update!

Related Article

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.