简体   繁体   English

如何在SQLite中检索多个last_insert_rows?

[英]How can I retrieve multiple last_insert_rows in SQLite?

In a large database, I'm using a very long query similar to this: 在大型数据库中,我使用了一个很长的查询,类似于以下内容:

INSERT INTO t VALUES (@p_1); SELECT last_insert_rowid() as i_1;
INSERT INTO t VALUES (@p_2); SELECT last_insert_rowid() as i_2;
...

This allows for infinitely faster insertion compared to single transactions. 与单笔交易相比,这可以无限快地插入。 However, I was surprised that only one value is returned instead of one per SELECT . 但是,我很惊讶仅返回一个值,而不是每个SELECT返回一个值。

Usually, this is a non-issue because it is easy to use only one rowid and calculate the others from it (autoincrement issues may be an exception). 通常,这不是问题,因为仅使用一个rowid并从中计算其他rowid很容易(自动递增问题可能是一个例外)。 However, for the sake of understanding (and disregarding that I probably abuse SQLite): Why is this happening, and how could I still use multiple SELECT s in a single transaction? 但是,出于理解的目的(并且无视我可能滥用SQLite):为什么会发生这种情况,又如何在单个事务中仍然使用多个SELECT

try with temp table like below 试试下面的临时表

PRAGMA temp_store = MEMORY;
CREATE TEMP TABLE TempIds (id INTEGER);

INSERT INTO t VALUES (@p_1); INSERT INTO TempIds VALUES(last_insert_rowid());
INSERT INTO t VALUES (@p_2);  INSERT INTO TempIds VALUES(last_insert_rowid());
...

SELECT id FROM TempIds;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM