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