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>