Keselme
Keselme

Reputation: 4279

Retrieve data according to timestamp value from Room Database

I'm trying to retrieve entries that were written in the last 2 minutes into the database. I use the following query:

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

However the result I get is the whole database.

What is wrong with this query?

Upvotes: 0

Views: 249

Answers (1)

Bob Snyder
Bob Snyder

Reputation: 38319

The SQLite date-time functions are described here.

If gatt_server_connection_timestamp is in milliseconds since epoch, this query should work:

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

Upvotes: 1

Related Questions