SQLite LAST_INSERT_ROWID() 函数返回当前会话数据库中最后一行 INSERT 的 ROWID。
语法
LAST_INSERT_ROWID()
参数
无需参数。
返回值
返回数据库中最后一行INSERT的ROWID对于当前会话
示例:
考虑下面的示例,其中创建了一个名为 Employee 的表。创建表后,LAST_INSERT_ROWID()函数用于在各种场景下获取该数据库最后一条INSERT记录的ROWID。
sqlite> CREATE TABLE Employee (
EmpID INTEGER PRIMARY KEY,
Name VARCHAR(50));
sqlite> SELECT LAST_INSERT_ROWID();
+---------------------+
| LAST_INSERT_ROWID() |
+---------------------+
| 0 |
+---------------------+
sqlite> INSERT INTO Employee(Name) Values ('John');
sqlite> INSERT INTO Employee(Name) Values ('Marry');
sqlite> SELECT LAST_INSERT_ROWID();
+---------------------+
| LAST_INSERT_ROWID() |
+---------------------+
| 2 |
+---------------------+
sqlite> INSERT INTO Employee(Name) Values ('Kim') , ('Jo');
sqlite> SELECT LAST_INSERT_ROWID();
+---------------------+
| LAST_INSERT_ROWID() |
+---------------------+
| 4 |
+---------------------+
sqlite> SELECT * FROM Employee;
+-------+-------+
| EmpID | Name |
+-------+-------+
| 1 | John |
| 2 | Marry |
| 3 | Kim |
| 4 | Jo |
+-------+-------+
sqlite> INSERT INTO Employee(Name) Values ('Ramesh');
sqlite> SELECT LAST_INSERT_ROWID();
+---------------------+
| LAST_INSERT_ROWID() |
+---------------------+
| 5 |
+---------------------+
sqlite> SELECT * FROM Employee;
+-------+--------+
| EmpID | Name |
+-------+--------+
| 1 | John |
| 2 | Marry |
| 3 | Kim |
| 4 | Jo |
| 5 | Ramesh |
+-------+--------+