我在房間資料庫 Android Studio 中有一個資料庫,我需要在其中找到與通過 http 查詢找到的日期匹配的記錄
我看到的問題是,由于我在嘗試使用“LIKE”或“=”進行查詢時使用了資料轉換器(ZoneDateTimeConverter),因此它回傳一個空物件......我該如何進行查詢?
查詢 1:
@Query("select * from Operation where date =:arg0")
Operation getOperationConsult(String arg0);
查詢 2:
@Query("select * from Operation where date LIKE :arg0")
Operation getOperationConsult(String arg0);
經營物體日期:
@ColumnInfo(name = "date")
ZonedDateTime date;
轉換器:
public class ZonedDateTimeTypeConverter {
@TypeConverter
public static ZonedDateTime toZonedDateTime(Long value) {
return value == null ? null : ZonedDateTime.ofInstant(Instant.ofEpochMilli(value), ZoneOffset.UTC);
}
@TypeConverter
public static Long toString(ZonedDateTime value) {
return value == null ? null : value.toInstant().toEpochMilli();
}
}
uj5u.com熱心網友回復:
簡而言之,您需要比較like for like,并且資料的存盤時間可以長到毫秒。
所以你可以有:-
@Query("SELECT * FROM operation WHERE date=:arg0")
List<Operation> getOperationConsult(long arg0);
可以通過使用您為 Room 提供的 TypeConverter 轉換為 long 的 ZonedDateTime 來確定 long 的位置(稍后會詳細介紹)。
但是,您正在處理毫秒,因此由于高精度,上述查詢可能根本沒有用。
也許考慮以下幾點:-
@Query("SELECT * FROM operation WHERE date / (1000 * 60 * 60 * 24)= (:arg0 / (1000 * 60 * 60 * 24))")
List<Operation> getOperationsConsultPerDate(long arg0);
這將去除時間(毫秒、秒、分鐘和小時)以僅保留日期(對于引數的雙方),因此將檢索與傳遞的時間具有相同日期的所有操作。
要確定長度,可以使用 ZDT 然后使用toStringTypeConverter來實作。
考慮以下作業示例,它使用您的操作物體(至少是日期列)和上述 dao(為了簡潔和方便,在主執行緒上運行):-
public class MainActivity extends AppCompatActivity {
private final static String TAG = "DBINFO";
TheDatabase db;
AllDao dao;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = TheDatabase.getInstance(this);
dao = db.getAllDao();
/* Add some data */
for (int i=0;i<48;i ) {
ZonedDateTime zdt = ZonedDateTime.now().minusHours(i).plusHours(24);
dao.insert(new Operation(zdt));
}
/* extract Operations based upon the current time */
logOperations(dao.getOperationConsult(ZonedDateTimeTypeConverter.toString(ZonedDateTime.now())));
logOperations(dao.getOperationsConsultPerDate(ZonedDateTimeTypeConverter.toString(ZonedDateTime.now())));
}
void logOperations(List<Operation> operationList) {
for(Operation o: operationList) {
Log.d(TAG,"Operation ID is " o.id " date is " o.date.toString());
}
}
}
運行上述結果:-
- 由于精度因素,第一個查詢沒有提取任何內容。
- 以下來自第二個查詢
:-
2021-12-15 10:49:16.451 D/DBINFOEXAMPLE2: Operation ID is 25 date is 2021-12-14T23:49:16.398Z
2021-12-15 10:49:16.451 D/DBINFOEXAMPLE2: Operation ID is 26 date is 2021-12-14T22:49:16.400Z
2021-12-15 10:49:16.452 D/DBINFOEXAMPLE2: Operation ID is 27 date is 2021-12-14T21:49:16.401Z
2021-12-15 10:49:16.452 D/DBINFOEXAMPLE2: Operation ID is 28 date is 2021-12-14T20:49:16.403Z
2021-12-15 10:49:16.452 D/DBINFOEXAMPLE2: Operation ID is 29 date is 2021-12-14T19:49:16.404Z
2021-12-15 10:49:16.452 D/DBINFOEXAMPLE2: Operation ID is 30 date is 2021-12-14T18:49:16.407Z
2021-12-15 10:49:16.452 D/DBINFOEXAMPLE2: Operation ID is 31 date is 2021-12-14T17:49:16.408Z
2021-12-15 10:49:16.453 D/DBINFOEXAMPLE2: Operation ID is 32 date is 2021-12-14T16:49:16.410Z
2021-12-15 10:49:16.453 D/DBINFOEXAMPLE2: Operation ID is 33 date is 2021-12-14T15:49:16.412Z
2021-12-15 10:49:16.453 D/DBINFOEXAMPLE2: Operation ID is 34 date is 2021-12-14T14:49:16.414Z
2021-12-15 10:49:16.453 D/DBINFOEXAMPLE2: Operation ID is 35 date is 2021-12-14T13:49:16.416Z
2021-12-15 10:49:16.453 D/DBINFOEXAMPLE2: Operation ID is 36 date is 2021-12-14T12:49:16.417Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 37 date is 2021-12-14T11:49:16.420Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 38 date is 2021-12-14T10:49:16.422Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 39 date is 2021-12-14T09:49:16.424Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 40 date is 2021-12-14T08:49:16.426Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 41 date is 2021-12-14T07:49:16.427Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 42 date is 2021-12-14T06:49:16.430Z
2021-12-15 10:49:16.454 D/DBINFOEXAMPLE2: Operation ID is 43 date is 2021-12-14T05:49:16.432Z
2021-12-15 10:49:16.455 D/DBINFOEXAMPLE2: Operation ID is 44 date is 2021-12-14T04:49:16.434Z
2021-12-15 10:49:16.455 D/DBINFOEXAMPLE2: Operation ID is 45 date is 2021-12-14T03:49:16.435Z
2021-12-15 10:49:16.455 D/DBINFOEXAMPLE2: Operation ID is 46 date is 2021-12-14T02:49:16.436Z
2021-12-15 10:49:16.455 D/DBINFOEXAMPLE2: Operation ID is 47 date is 2021-12-14T01:49:16.438Z
2021-12-15 10:49:16.455 D/DBINFOEXAMPLE2: Operation ID is 48 date is 2021-12-14T00:49:16.439Z
額外的
As the stored date (if divided by 1000) is a format (format 12) that the SQLite date and time functions recognises, then you can use SQlite date functions.
- note that storing the data as a long is, as far as the database is concerned, the most efficient format taking up up to 8 bytes rather than over double that to store the same information as a string/text format.
As an example the datetime function returns the datetime in the format yyyy-mm-dd hh:mm:ss, so to demonstrate consider the following query:-
@Query("SELECT datetime(date / 1000,'unixepoch') FROM operation WHERE date(date / 1000,'unixepoch') = date('now')")
List<String> getDatesFromOperations();
這將回傳每個選定操作的日期和時間,例如2021-12-15 00:20:18。所選操作的日期是呼叫/使用查詢的日期(即“現在”)。查詢不會回傳房間物件,但因為只有一個輸出列,然后 Room 可以處理常見型別的串列,在這種情況下是 String。
- unixepoch 表明該值是格式 12 的 unix 格式,而不是儒略日數。
所以使用:-
for (String s: dao.getDatesFromOperations()) {
Log.d(TAG "EXAMPLE3","DateTime is " s);
}
會導致:-
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 00:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 01:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 02:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 03:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 04:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 05:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 06:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 07:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 08:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 09:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 10:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 11:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 12:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 13:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 14:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 15:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 16:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 17:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 18:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 19:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 20:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 21:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 22:20:18
D/DBINFOEXAMPLE3: DateTime is 2021-12-15 23:20:18
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/382586.html
標籤:爪哇 安卓工作室 android-room 数据转换 分区日期时间
上一篇:如何避免回收站從頭頂回傳
