Search code examples
androidsqlsqliteandroid-roomstrftime

How to use parameter in strftim() function in sqlite


I'm using Room library in an android project. How can I use the method parameter in strftime() function (something like :period days instead of 7 days)?

@Query("SELECT * FROM purchase " +
        "WHERE date >= strftime('%s', 'now', 'localtime', 'start of day', '7 days')")
List<Purchase> getPurchases(int period);                               \\ ☝ //

Solution

  • With string concatenation:

    strftime('%s', 'now', 'localtime', 'start of day', :period || ' days')