简体   繁体   English

如何使用sqlite在插入查询中获取插入的行ID?

[英]How to get the inserted row id in insert query using sqlite?

INSERT INTO Student (id, FirstName, LastName, SpouseName, DOB, GenderId, Email ) OUTPUT Inserted.id VALUES (hex( randomblob(4)) || '-' || hex( randomblob(2))|| '-' || '4' || substr( hex( randomblob(2)), 2) || '-'|| substr('AB89', 1 + (abs(random()) % 4) , 1) ||substr(hex(randomblob(2)), 2) || '-' || hex(randomblob(6)),?, ?, ?, ?, (SELECT ID FROM Gender WHERE Name ='Female'), ?; INSERT INTO Student(id,FirstName,LastName,SpouseName,DOB,GenderId,Email)输出Inserted.id值(hex(randomblob(4))||'-'|| hex(randomblob(2))||'-' ||'4'|| substr(hex(randomblob(2)),2)||'-'|| substr('AB89',1 +(abs(random())%4),1)|| substr (hex(randomblob(2)),2)||'-'|| hex(randomblob(6)),?,?,?,?,(在性别名称为'Female'的情况下选择ID),?;

OUTPUT Inserted.id ll retrieve last inserted row id in SQl. OUTPUT Inserted.id将检索SQl中最后插入的行ID。 But, Sqlite I don't Know what's instead of OUTPUT Inserted.id. 但是,Sqlite我不知道是什么,而不是OUTPUT Inserted.id。

Thanks in Advance. 提前致谢。

You need to split this into two statements; 您需要将其分为两个语句。 first is your insert and then immediately after that SELECT last_insert_rowid() which will give you the inserted rows' id. 首先是您的插入内容,然后紧接着是SELECT last_insert_rowid() ,它将为您提供插入的行的ID。

See the documentation on how this works; 请参阅有关其工作原理的文档 and the sql reference for the function as well. 以及该函数的sql参考

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

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