小编典典

在SQLite中插入多行

sql

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

这里得到的想法,这是我的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",
       "Espoir",
       "False",
UNION
SELECT "Leonard Collections",
       "Instrumental",
       "00:03:51.6770000",
       "1/1/0001 12:00:00 AM",
       "Don't Cry For My Argentina",
       "False"

但它抛出

SQL logic error or missing database

near "UNION": syntax error

这是我的表结构

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
)

我的查询有问题吗?

任何帮助,将不胜感激。

TIA


阅读 321

收藏
2021-05-05

共1个答案

小编典典

由于提到了最新版本的SQLite,因此应使用多值插入(自3.7.11版以来受SQLite支持),如下所示:

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

这是更短,更快,更不容易出错的方法。其他一些数据库(至少MySQL和PostgreSQL)也支持此语法。

2021-05-05