Upgrade Android databases and Android Databases

Source: Internet
Author: User

Upgrade Android databases and Android Databases

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, "              + "agetext, )";        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 as above, we know that a. db database file will be generated in the local directory database! However, when we encounter version iterations, we often need to do more. At this time, we will create a new database.
At this time, after compilation, we found that a corresponding table was not generated in the corresponding directory of the database. Of course, when debugging the code, we can clearly understand that when the application data is re-run, the onCreate method will be re-executed, and of course the tables that have not been successfully generated will be generated, you can never know the data every time you update the application iteratively, right! Therefore:
<pre name="code" class="java">@Override  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {    onCreate(db);  } 
The main idea is to update the version, that is, when the version of your existing app is 1, update the value of version> 1 during iterative version update so that we will execute the onUpgrade () method when new MySQLiteHelper, then, write the corresponding operation in this method to produce the corresponding table in the database without the need for the user to clear the data of the application.
 
 
Of course, a major drawback of this method is that we may forget to modify the version when releasing the version, which leads to a series of untidy bugs caused by the inability to operate data. Therefore, LitePal is recommended for database operations! For more information, see <a target = _ blank href = "http: // http://blog.csdn.net/guolin_blog/article/details/39151617"> click to open the link </a>

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.