Database Upgrade in Android

Source: Internet
Author: User

public class Mysqlitehelper extends Sqliteopenhelper {public            static final String <span style= "font-family:arial, Helvetica, Sans-serif; " >sql_create</span><span style= "font-family:arial, Helvetica, Sans-serif;" >= "CREATE TABLE News ("  </span>            + "_id Integer primary Key AutoIncrement,"              + "Nametext,"              + "AG EText,) ";        Public Mysqlitehelper (Context context, String name, Cursorfactory factory,              int version) {          Super (context, name, Factory, version);      }        @Override public      void OnCreate (Sqlitedatabase db) {          db.execsql (sql_create);     }        @Override public      void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {      }    
When we create a sqlitehelper, we know that a. db database file will be generated in the local directory. But we're going to have to do more with version iterations and then create a new database.
At this point, we found that we did not generate a corresponding table in the database directory after the completion of the compilation. Of course, when we debug the code, we can clearly apply the data so that the re-operation of the OnCreate method will be executed once again that will certainly not generate a successful table generated, you can update the application when the user iteration is not always clear the data is it! So:
<pre name= "code" class= "Java" > @Override public  void Onupgrade (sqlitedatabase db, int oldversion, int newversion) {    onCreate (db);  
The main idea is to update version, that is, your user's existing app on version is 1, when the iteration version is updated to update version >1 value so that we in New Mysqlitehelper () The Onupgrade () method is executed, and the corresponding operation is written in the method to produce the corresponding table in the database without requiring the user to clear the application data.
Of course, one of the serious drawbacks of this approach is that  we may forget to modify the version when we release it, causing the inability to manipulate the data to cause a series of unmanageable bugs. So it is recommended to use Litepal Open Source Library for database operation! Details visible  <a target=_blank href= "http://http://blog.csdn.net/guolin_blog/article/details/39151617" > Click to open Link </a>

Database Upgrade in Android

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.