在MySQL中,你可以像这样插入多行:
INSERT INTO 'tablename' ('column1', 'column2') VALUES
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2');
然而,当我尝试这样做时,我得到了一个错误。是否可以在SQLite数据库中一次插入多行?这样做的语法是什么?
在MySQL中,你可以像这样插入多行:
INSERT INTO 'tablename' ('column1', 'column2') VALUES
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2'),
('data1', 'data2');
然而,当我尝试这样做时,我得到了一个错误。是否可以在SQLite数据库中一次插入多行?这样做的语法是什么?
当前回答
Alex是正确的:“select…”“Union”语句将失去对某些用户非常重要的排序。即使当您以特定的顺序插入时,sqlite也会更改内容,因此如果插入顺序很重要,则更倾向于使用事务。
create table t_example (qid int not null, primary key (qid));
begin transaction;
insert into "t_example" (qid) values (8);
insert into "t_example" (qid) values (4);
insert into "t_example" (qid) values (9);
end transaction;
select rowid,* from t_example;
1|8
2|4
3|9
其他回答
是的,从SQLite 3.7.11开始,SQLite支持这一点。从SQLite文档:
(当这个答案最初写的时候,这是不支持的)
为了兼容旧版本的SQLite,你可以使用andy和fearless_fool所建议的技巧,使用UNION,但对于3.7.11及以后版本,这里描述的更简单的语法应该是首选。
Alex是正确的:“select…”“Union”语句将失去对某些用户非常重要的排序。即使当您以特定的顺序插入时,sqlite也会更改内容,因此如果插入顺序很重要,则更倾向于使用事务。
create table t_example (qid int not null, primary key (qid));
begin transaction;
insert into "t_example" (qid) values (8);
insert into "t_example" (qid) values (4);
insert into "t_example" (qid) values (9);
end transaction;
select rowid,* from t_example;
1|8
2|4
3|9
我有一个像下面这样的查询,但与ODBC驱动程序SQLite有一个错误“,”它说。 我运行vbscript在HTA (Html应用程序)。
INSERT INTO evrak_ilac_iliskileri (evrak_id, ilac_id,
baglayan_kullanici_id, tarih) VALUES (4150,762,1,datetime()),
(4150,9770,1,datetime()),(4150,6609,1,datetime()),(4150,3628,1,datetime()),
(4150,9422,1,datetime())
是的,sql可以做到这一点,但是使用不同的语法。顺便说一下,sqlite文档非常好。它还会告诉您插入几行的唯一方法是使用select语句作为要插入的数据的源。
使用事务的问题是,您还锁定了用于读取的表。所以,如果你有很多数据要插入,你需要访问你的数据,例如预览之类的,这种方式就不太适用了。
另一个解决方案的问题是,您失去了插入的顺序
insert into mytable (col)
select 'c'
union
select 'd'
union
select 'a'
union
select 'b';
在sqlite中,数据将被存储为a,b,c,d…