SQL 如何在 SQLite 3 表中插入多行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10038759/
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 15:13:37  来源:igfitidea点击:

How to insert multiple rows into a SQLite 3 table?

sqlsqliteinsertbulkinsert

提问by Ivan

In MySQL I'd use

在 MySQL 中我会使用

INSERT INTO `mytable` (`col1`, `col2`) VALUES
  (1, 'aaa'),
  (2, 'bbb');

but this causes an error in SQLite. What is the correct syntax for SQLite?

但这会导致 SQLite 出错。SQLite 的正确语法是什么?

回答by Leo Correa

This has already been answered before here: Is it possible to insert multiple rows at a time in an SQLite database?

这已经在这里之前得到了回答:Is it possible to insert multiple rows in a SQLite database?

To answer your comment to OMG Ponies answer:

要回答您对 OMG Ponies 的评论,请回答:

As of version 3.7.11 SQLite does support multi-row-insert. Richard Hipp comments:

从 3.7.11 版本开始,SQLite 确实支持多行插入。理查德·希普评论:

"The new multi-valued insert is merely syntactic suger (sic) for the compound insert. 
There is no performance advantage one way or the other."

回答by OMG Ponies

Use a UNION:

使用联合:

INSERT INTO `mytable` 
 (`col1`, `col2`) 
SELECT 1, 'aaa'
UNION ALL
SELECT 2, 'bbb'

UNION ALLis quicker than UNION, because UNIONremoves duplicates -- UNION ALLdoes not.

UNION ALL比 快UNION,因为UNION删除重复项 -UNION ALL不会。

回答by mjb

Start from version 2012-03-20 (3.7.11), sqlite support the following INSERT syntax:

从 2012-03-20 (3.7.11) 版本开始,sqlite 支持以下 INSERT 语法:

INSERT INTO 'tablename' ('column1', 'column2') VALUES
  ('data1', 'data2'),
  ('data3', 'data4'),
  ('data5', 'data6'),
  ('data7', 'data8');

Read documentation: http://www.sqlite.org/lang_insert.html

阅读文档:http: //www.sqlite.org/lang_insert.html

SQLite INSERT Statement Diagram

SQLite INSERT 语句图