SQL 在 SQLite 中插入多行

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

Insert multiple rows in SQLite

sqlsqlite

提问by Vincent Dagpin

I am trying to insert multiple rows in a SQLite (latest version) table but throws an error

我正在尝试在 SQLite(最新版本)表中插入多行但引发错误

got the idea from HEREand here is my sql query :

这里得到了这个想法,这是我的 sql 查询:

INSERT INTO "Track"
SELECT "Leonard Collections" AS "Album",
       "Instrumental" AS "Artist",
       "00:02:59.3800000" AS "Duration",
       "1/1/0001 12:00:00 AM" AS "ReleasedDate",
       "If You Love Me" AS "Title",
       "False" AS "IsPlayableOnLocal"
UNION
SELECT "Leonard Collections",
       "Instrumental",
       "00:02:56.6930000",
       "1/1/0001 12:00:00 AM",
       "Esptheitroad",
       "False",
UNION
SELECT "Leonard Collections",
       "Instrumental",
       "00:03:51.6770000",
       "1/1/0001 12:00:00 AM",
       "Don't Cry For My Argentina",
       "False"

but it throws

但它抛出

SQL logic error or missing database

near "UNION": syntax error

this is my Table Structure

这是我的表结构

CREATE TABLE Track 
(
    ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , 
    Album VARCHAR(100) NULL , 
    Artist VARCHAR(255) NOT NULL DEFAULT "Artist Unknown", 
    Duration VARCHAR(255) NOT NULL , 
    LocalPath VARCHAR(255) NULL , 
    ReleasedDate DATE NOT NULL , 
    Title VARCHAR(255) NULL , 
    IsPlayableOnLocal INTEGER NOT NULL , 
    Rating VARCHAR(255) NULL
)

is there a problem with my query?

我的查询有问题吗?

any help would be appreciated.

任何帮助,将不胜感激。

TIA

TIA

回答by mvp

Since you mention latest version of SQLite, you should use multi-valued insert (supported by SQLite since version 3.7.11), like this:

由于您提到了最新版本的 SQLite,您应该使用多值插入(SQLite 自 3.7.11 版起支持),如下所示:

INSERT INTO mytable (col1, col2, col3) VALUES
    (1, 2, "abc"),
    (2, 4, "xyz"),
    (3, 5, "aaa"),
    (4, 7, "bbb");

This is shorter, faster and less prone to errors. This syntax is also supported by some other databases (at least MySQL and PostgreSQL).

这更短、更快并且更不容易出错。其他一些数据库(至少 MySQL 和 PostgreSQL)也支持这种语法。

回答by Devolus

In your second union statement you have superflous ',' character after "False". That is most likely the problem.

在您的第二个联合声明中,您在“False”之后有多余的 ',' 字符。这很可能是问题所在。