How can an insert return the ID assigned to a new experiment?

RETURNING sends selected fields from modified rows to the caller, including an automatically assigned key.

SQLite example and interpretation

Inserting into an empty table returns the new experiment ID. For multi-row inserts return a request identifier rather than assuming output order matches the input.

CREATE TABLE run(id INTEGER PRIMARY KEY,tag TEXT);
INSERT INTO run(tag) VALUES('trial') RETURNING id,tag;

Result [[1, "trial"]]

中文

插入新实验时如何在同一条语句拿到分配的编号?

RETURNING 将本次修改行的所选字段返回给调用方,可以直接读取分配的主键。

SQLite 示例与适用条件

空表插入一次得到本次实验的编号。多行插入时不要根据返回顺序映射业务请求,应一并返回请求标识。

CREATE TABLE run(id INTEGER PRIMARY KEY,tag TEXT);
INSERT INTO run(tag) VALUES('trial') RETURNING id,tag;

结果 [[1, "trial"]]

References / 参考来源

Keep reading / 继续阅读