Elegant handling of Android database upgrade issues

Source: Internet
Author: User

Originally completed on: 2015-04-27 19:28:22

Provides a way to gracefully handle Android database upgrade issues directly on the code:

1 packageCom.example.databaseissuetest; 2 3 ImportAndroid.content.Context; 4 ImportAndroid.database.sqlite.SQLiteDatabase; 5 ImportAndroid.database.sqlite.SQLiteOpenHelper; 6 ImportAndroid.text.TextUtils; 7 ImportAndroid.util.Log; 8 9 public class Databasehelper extendsSqliteopenhelper {Ten public static final String db_name = "Jjtest"; one-to-one static final string[] Col_sqls ={"CREATE TABLE Jjtest (ID integer primary key autoincrement, name text, age int)", "ALTER TABLE Jjtest Add class text", "ALTER TABLE jjtest add friends integer default 3" , };17 public  databasehelper (Context C Ontext) {(context, db_name, null , col_sqls.length); LOG.E ("David", "Databasehelper" ); }  @Override24 public void  onCreate (sqlitedatabase db) {LOG.E ("David", "OnCreate" ), and Onupgrade ( DB, 0 , col_sqls.length), oldversion }28  @Override30 public void Onupgrade (sqlitedatabase db, int, int  newversion) {log.e ("David", "Onupgrade oldversion =" +  oldversion), LOG.E ("David", "Onupgrade newve rsion = "+  newversion); (int i = oldversion; i < col_sqls.length; i++ ) {String sql =  col_sqls[i];35 if (!  Textutils.isempty (SQL)) { db.execsql (SQL), PNS }38 }39 }40}    / span>                 

Elegant handling of Android database upgrade issues

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.