更新时间:2023-12-01 13:50:58
SELECT
语句中不能使用 INSERT
.您应该先插入,然后使用最后插入的 id:
You can't use INSERT
in SELECT
statement. You should first insert and then use last inserted id:
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT last_insert_rowid()));
由于您想插入许多带有父 ID 的记录,这里有一个解决方法:
Since you want to insert many records with parent ID, here is a workaround:
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE IF NOT EXISTS temp(id integer);
DELETE FROM temp;
INSERT INTO ParentTable (Col1) VALUES( 'test');
INSERT INTO temp SELECT last_insert_rowid();
INSERT INTO ChildTable (col1, col2, ParentId)
VALUES(1,2, (SELECT id FROM temp LIMIT 1));
.............
COMMIT;
DROP TABLE temp;
或者您可以为此创建一个永久表.
Or you can create a permanent table to this effect.