首页 > 解决方案 > 根据房间数据库中的时间戳值检索数据

问题描述

我正在尝试检索在最后 2 分钟内写入数据库的条目。我使用以下查询:

@Query("SELECT * FROM Contacts where ('now()' - gatt_server_connection_timestamp) <= 120000")
    List<Contact> getContactsByGattServerConnectionTimestamp(); 

但是我得到的结果是整个数据库。

这个查询有什么问题?

标签: android-room

解决方案


SQLite 日期时间函数在此处描述

如果gatt_server_connection_timestamp自纪元以来以毫秒为单位,则此查询应该有效:

@Query("SELECT * FROM Contacts where gatt_server_connection_timestamp >= (1000 * strftime('%s', datetime('now', '-2 minutes'))))
    List<Contact> getContactsByGattServerConnectionTimestamp();

推荐阅读