in sqlite3, can a select succeed within a transaction of insert?

前端 未结 2 1009
逝去的感伤
逝去的感伤 2021-01-17 15:10

I begin a transaction, which is to insert several records into a table. Can I select the latest inserted record out of the database before the transaction commit?

2条回答
  •  别那么骄傲
    2021-01-17 15:20

    Yes, during or after the transaction you can use the last_insert_rowid() function.

    The last_insert_rowid() function returns the ROWID of the last row inserted from the database connection which invoked the function.

    In other words:

    SQLite version 3.6.23
    Enter ".help" for instructions
    Enter SQL statements terminated with a ";"
    
    sqlite> create table T (C);
    sqlite> insert into T values ('hello');
    sqlite> select last_insert_rowid();
    1
    sqlite> BEGIN;
    sqlite> insert into T values ('test 2');
    sqlite> select last_insert_rowid();
    2
    sqlite> select rowid,* from T;
    1|hello
    2|test 2
    sqlite> ROLLBACK;
    sqlite> select last_insert_rowid();
    2
    sqlite> select rowid,* from T;
    1|hello
    sqlite>
    

提交回复
热议问题