SQLite add column, keep data

后端 未结 4 816
眼角桃花
眼角桃花 2021-01-31 09:19

I have a database with 4 columns:

@Override
public void onCreate(SQLiteDatabase database) {
        database.execSQL(\"CREATE TABLE \" + DATABASENAME + \" (name          


        
4条回答
  •  猫巷女王i
    2021-01-31 10:11

    Please see this page for the syntax to create a new column on a table. Basically it is:

    ALTER TABLE mytable ADD COLUMN mycolumn TEXT
    

    In your onUpgrade method, it would look something like this:

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        String upgradeQuery = "ALTER TABLE mytable ADD COLUMN mycolumn TEXT";
        if (oldVersion == 1 && newVersion == 2)
             db.execSQL(upgradeQuery);
    }
    

提交回复
热议问题