sqlite alter table add MULTIPLE columns in a single statement

前端 未结 4 602
南旧
南旧 2020-11-27 13:13

Is it possible to alter table add MULTIPLE columns in a single statement in sqlite? The following would not work.

alter table test add column mycolumn1 text, add          


        
4条回答
  •  南笙
    南笙 (楼主)
    2020-11-27 13:51

    The answer from @mu is too short' is right. As an extra, adding an optimized workaround for adding multiple columns using the benefit of transactions in SQL.

    String alterTableQuery = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN ";
    List newColumns = ..// Your new columns
    
    db.beginTransaction();
    for (String column : newColumns){
        db.execSQL(alterTableQuery + column +  " VARCHAR");
    }
    db.setTransactionSuccessful();
    db.endTransaction();
    

    I hope this will help someone.

提交回复
热议问题