I add new column on my database (i have data on this database)
I have this code:
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
db.execSQL("ALTER TABLE MEN ADD COLUMN New_Pulse INTEGER DEFAULT 0");
}
}
but it wont work, from where I get the oldVersion and the newVersion ?
For now I done this:
try{
db.execSQL("ALTER TABLE MEN ADD COLUMN New_Pulse INTEGER DEFAULT 0");
}
catch (Exception e) {
// TODO: handle exception
}
it work for me - but I know that its Ugly solution
You don't have to do the check yourself; SQLiteOpenHelper does that automatically! When you enter a higher version number than before, the helper calls onUpgrade()
method, where you should handle what to do on upgrade.
....
....
mySqlHelper = new mySqlHelper (context, DATABASE_NAME, null, DATABASE_VERSION);
private static class MySQLHelper extends SQLiteOpenHelper {
public MySqlHelper(Context context, String name, CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// upgrade code..
// drop tables
onCreate(db);
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.