简体   繁体   English

SQLite 行数据的逻辑时钟,例如 SQL 服务器中的 rowversion

[英]SQLite logical clock for row data like rowversion in SQL Server

Does SQLite have anything like SQL Server's rowversion column that will increment every time a row changes? SQLite 是否有类似 SQL 服务器的rowversion 列,每次行更改时都会增加? Essentially, I want to have a logical clock for each of my tables that updates whenever a table updates.本质上,我希望每个表都有一个逻辑时钟,每当表更新时都会更新。 With this logical clock, my application can hold the version it most recently saw, and can only re-fetch if data has changed.有了这个逻辑时钟,我的应用程序可以保存它最近看到的版本,并且只有在数据发生变化时才能重新获取。

I could implement this with something like:我可以用类似的东西来实现它:

CREATE TRIGGER FOO_VERSION_INSERT_TRIGGER 
  AFTER INSERT ON FOO FOR EACH ROW
  BEGIN
    UPDATE CLOCKS
    SET VERSION = (
      SELECT IFNULL(MAX(VERSION), 0) + 1 FROM CLOCKS
    )
    WHERE TABLE_NAME = "FOO"
  END

CREATE TRIGGER FOO_VERSION_UPDATE_TRIGGER 
  AFTER UPDATE ON FOO FOR EACH ROW
  BEGIN
    UPDATE CLOCKS
    SET VERSION = (
      SELECT IFNULL(MAX(VERSION), 0) + 1 FROM CLOCKS
    )
    WHERE TABLE_NAME = "FOO"
  END

CREATE TRIGGER FOO_VERSION_DELETE_TRIGGER 
  AFTER INSERT ON FOO FOR EACH ROW
  BEGIN
    UPDATE CLOCKS
    SET VERSION = (
      SELECT IFNULL(MAX(VERSION), 0) + 1 FROM CLOCKS
    )
    WHERE TABLE_NAME = "FOO"
  END

But this seems like something that should natively exist already.但这似乎本来就应该存在。

You can use PRAGMA data_version to see if any other connection to the database modified it and your program thus needs to refresh data.您可以使用PRAGMA data_version查看是否有任何其他与数据库的连接修改了它,因此您的程序需要刷新数据。

The integer values returned by two invocations of "PRAGMA data_version" from the same connection will be different if changes were committed to the database by any other connection in the interim.如果在此期间通过任何其他连接将更改提交到数据库,则从同一连接两次调用“PRAGMA data_version”返回的 integer 值将不同。

It won't tell you which specific tables were changed, though.但是,它不会告诉您更改了哪些特定表。

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

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