room/SQLite 后台 scheduler/trigger 自动运行

room/SQLite background scheduler/trigger that runs automatically

我有样品table:

我有一个示例 table,它的属性是“creationDate”。我想要的是一种自创建日期起每 24 小时递增(更新)“numOfTimesUpdated”属性的方法。所以假设“创建日期”是 01.01.2021 12:12 AM => numOfTimesUpdated=0, 02.01.2021 12:12 AM => numOfTimesUpdated=1, 03.01.2021 12:12 AM => numOfTimesUpdated= 3.

我怎样才能以最好的方式实施这样的事情?

SQLite 是否有某种背景 scheduler/trigger 可以自动调用 UPDATE 查询?或者我唯一的机会是客户端(应用程序)使用 smth。像 ApplicationManager?

How can I implement something like this in the best way? You don't appear to even need a numberOfTimesUpdated column as the number of days since the creationDate can be calculated when required.

  • 如果 date/time 以支持的格式存储(例如 YYYY-MM-DD HH:MM),则非常简单。

例如考虑这个:-

DROP TABLE IF EXISTS table1;
CREATE TABLE IF NOT EXISTS table1 (id INTEGER PRIMARY KEY, name TEXT, creationdate TEXT);
INSERT INTO table1 VALUES
    (null,'myname','2021-01-02'),(null,'anothername','2021-03-03'),(null,'andanother','2021-06-06')
;
SELECT *,strftime('%s','now')/(60 * 60 * 24) - strftime('%s',creationdate)/(60 * 60 * 24) AS numOfTimesUpdated FROM table1;
DROP TABLE IF EXISTS table1;

它:-

  1. 如果 table 存在则删除
  2. 创建 table
  3. 插入 3 行创建日期(2021 年 1 月 21 日、2021 年 3 月 3 日和 2021 年 6 月 6 日)
  4. 提取所有行加上计算列,其中包含自创建日期以来的天数。
  5. 通过删除 table.
  6. 清理测试环境

2021 年 6 月 13 日 运行 的结果是:-

Does SQLite has some kind of background scheduler/trigger where a UPDATE Query gets automatically called?

不基于时间。

Or Is my only chance the client side(application) using smth. like an ApplicationManager?

是的,但您似乎也不需要这个。

工作室示例

下面是实现上面SQLite例子的工作室例子:-

表 1 实体 :-

@Entity(tableName = "table1")
public class Table1 {
    @PrimaryKey
    Long id;
    String name;
    String creationDate;

    public Table1(){}
    @Ignore
    public Table1(String name, String creationDate) {
        this.name = name;
        this.creationDate = creationDate;
    }
}
  • 注意,理论上 id 可以是 long long 而不是 int 已被使用。只要 long 必须有一个值 Long 已用于允许自动生成的 id(没有低效的 AUTOGENERATE)。

A POJO Table1WithNumberOfUpdates 获取带有附加计算列的 Table1:-

public class Table1WithNumberOfUpdates {
    @Embedded
    Table1 table1;
    int numOfTimesUpdated;
}

A Dao AllDao 允许插入和提取 Table1WithNumberOfUpdates 个对象的列表 :-

@Dao
interface AllDao {

    @Insert
    long insert(Table1 table1);
    @Query("SELECT *, strftime('%s','now')/(60 * 60 * 24) - strftime('%s',creationdate)/(60 * 60 * 24) AS numOfTimesUpdated FROM table1")
    List<Table1WithNumberOfUpdates> getTable1WithNumberOfUpdatesList();
}

一个标准的@Database returns 一个数据库实例:-

@Database(entities = {Table1.class},exportSchema = false,version = 1)
abstract class TheDatabase extends RoomDatabase {
    abstract AllDao getAllDao();

    private static volatile TheDatabase instance;

    public static TheDatabase getInstance(Context context) {
        if (instance == null) {
            instance = Room.databaseBuilder(
                    context,
                    TheDatabase.class,
                    "state.db"
            )
                    .allowMainThreadQueries()
                    .build();
        }
        return instance;
    }
}

最后 Activity 中的一些代码将三行相加并提取结果并将其输出到日志 :-

public class MainActivity extends AppCompatActivity {

    TheDatabase db;
    AllDao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //Instantiate Database and get dao
        db = TheDatabase.getInstance(this);
        dao = db.getAllDao();

        dao.insert(new Table1("myname","2021-01-02"));
        dao.insert(new Table1("anothername","2021-03-03"));
        dao.insert(new Table1("andanothername","2021-06-06"));
        for (Table1WithNumberOfUpdates t: dao.getTable1WithNumberOfUpdatesList()) {
            Log.d("TABLE1INFO","Name is " + t.table1.name + " Created = " + t.table1.creationDate + " Updates Since Creation = " + t.numOfTimesUpdated);
        }
    }
}

结果 :-

2021-06-14 10:17:44.498 D/TABLE1INFO: Name is myname Created = 2021-01-02 Updates Since Creation = 163
2021-06-14 10:17:44.499 D/TABLE1INFO: Name is anothername Created = 2021-03-03 Updates Since Creation = 103
2021-06-14 10:17:44.499 D/TABLE1INFO: Name is andanothername Created = 2021-06-06 Updates Since Creation = 8
  • 由于设备使用本地时区而不是上面使用的 SQLite 工具(Navicat for SQLite),因此多了 1 天。