【问题标题】:The last inserted rowid of a specific table in SQLite3SQLite3中特定表的最后插入的rowid
【发布时间】:2019-09-09 22:51:23
【问题描述】:

有没有办法在 SQLite3 中获取特定表的最后插入的 rowid?有一个函数last_insert_rowid 将最近一次成功INSERT 的rowid 返回到rowid 表或虚拟表中。但是,我想知道是否有这种方法可以获取特定表的最后插入的rowid。

【问题讨论】:

  • 不可靠,不。 SELECT max(rowid) FROM yourtable 通常会在没有插入具有用户指定的 rowid 值的行时起作用。

标签: sql sqlite insert rowid


【解决方案1】:

所有表的最后插入的rowids 存储在internal table sqlite_sequence 中,您可以像查询任何其他表一样查询它。确保您的主键 ID 具有属性 autoincrement,否则不会在此处列出。

sqlite> create table Test1 (id integer primary key autoincrement, text string);
sqlite> create table Test2 (id integer primary key autoincrement, text string);

[...]  -- Insert rows here

sqlite> select rowid from Test1;
id        
----------
1         
2         
sqlite> select rowid from Test2;
id        
----------
1         
2         
3
sqlite> select * from sqlite_sequence;
name        seq       
----------  ----------
Test1       2         
Test2       3         

【讨论】:

  • 有根据的猜测是:不,因为这些不受 SQLite 引擎控制。但老实说我不知道​​,因为我从未使用过虚拟表,没有测试环境来为您调查这个问题,也没有找到处理这个细节的资源。所以,与其等待一个可能永远不会到来的答案,不如自己去寻找答案?如果您实际使用虚拟表,则需要三个 SQL 语句才能获得第一个想法。
猜你喜欢
  • 1970-01-01
  • 2012-08-20
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多