SQL sqlite alter table 在单个语句中添加多个列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6172815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:47:48  来源:igfitidea点击:

sqlite alter table add MULTIPLE columns in a single statement

sqlsqlitealter-table

提问by user775187

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

是否可以在sqlite的单个语句中更改表添加多个列?以下是行不通的。

alter table test add column mycolumn1 text, add column mycolumn2 text;

回答by mu is too short

No, you have to add them one at a time. See the syntax diagram at the top of SQLite's ALTER TABLE documentation:

不,您必须一次添加一个。请参阅SQLite 的 ALTER TABLE 文档顶部的语法图:

ALTER TABLE syntax

ALTER TABLE 语法

There's no loop in the ADDbranch so no repetition is allowed.

ADD分支中没有循环,因此不允许重复。

回答by Arun P M

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.

@mu 的回答太短了' 是对的。另外,添加优化的解决方法以使用 SQL 中的事务的优势添加多个列。

String alterTableQuery = "ALTER TABLE " + TABLE_NAME + " ADD COLUMN ";
List<String> 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.

我希望这会帮助某人。

回答by anand arun

alter table test add column mycolumn1 text; alter table test add column mycolumn2 text;

更改表测试添加列 mycolumn1 文本;更改表测试添加列 mycolumn2 文本;

use the above redifined query

使用上面重新定义的查询