0

我被要求在一个表中添加 8 行。

insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '101', '5', null), ('207', '102', '5', null),
       ('207', '103', '5', null), ('207', '104', '5', null),
       ('207', '105', '5', null), ('207', '106', '5', null),
       ('207', '107', '5', null), ('207', '108', '5', null)

此操作适用于添加一个值,但添加多个时会出现错误

查询未能执行:靠近“,”:语法错误

什么不见​​了?

4

4 回答 4

3

迟到的答案 如果您使用的是 SQLITE 3.7.11或更高版本,则可以通过这种语法插入多行,

最简单的方法

INSERT INTO Rating (rID, mID, stars, ratingDate) VALUES ('207', '102', '5', null) , ('207', '102', '5', null) , ('207', '102', '5', null)

如果使用新的 SQLITE 版本,上面发布的有问题的子句确实有效。

选择条款

insert into Rating 
        SELECT '207' AS rID, '101' AS mID, '5' AS stars, null AS ratingDate   
  UNION SELECT '207', '102', '5', null
  UNION SELECT '207', '103', '5', null
  UNION SELECT '207', '104', '5', null
  UNION SELECT '207', '105', '5', null
  UNION SELECT '207', '106', '5', null            
  UNION SELECT '207', '107', '5', null
  UNION SELECT '207', '108', '5', null

或 SQL 是

insert into Rating (rID, mID, stars, ratingDate)
        SELECT '207', '101', '5', null 
  UNION SELECT '207', '102', '5', null
  UNION SELECT '207', '103', '5', null
  UNION SELECT '207', '104', '5', null
  UNION SELECT '207', '105', '5', null
  UNION SELECT '207', '106', '5', null            
  UNION SELECT '207', '107', '5', null
  UNION SELECT '207', '108', '5', null

请记住,我不想检查上述插入值集中的重复项,然后使用它来UNION ALL代替,UNION因为它会快一点。

于 2013-10-28T04:30:31.220 回答
0

我在 sql lite 中创建了表。建表脚本如下

 create table Rating (rID varchar(10),mID varchar(10),stars varchar(10),ratingDate date);

我使用以下查询插入到上表中,它对我来说工作正常。

insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '101', '5', null), ('207', '102', '5', null),
       ('207', '103', '5', null), ('207', '104', '5', null),
       ('207', '105', '5', null), ('207', '106', '5', null),
       ('207', '107', '5', null), ('207', '108', '5', null);
于 2013-01-25T17:22:19.410 回答
0

我假设您的 RDBMS 不支持这种结构。

insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '101', '5', null);
insert into Rating ( rID, mID, stars, ratingDate ) 
values ('207', '102', '5', null);
.....
于 2013-01-25T16:59:49.127 回答
0

我建议:

insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '101', '5', null);
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '102', '5', null);
...
insert into Rating ( rID, mID, stars, ratingDate ) values ('207', '108', '5', null);
于 2013-01-25T17:00:09.833 回答