我有一个数据库,其中有一个表格记录了车辆最后一次加油的时间。它有一个整数类型的日期列。
我需要能够确定最后一次加油日期。我正在使用:
Cursor cursor = db.query(FUEL_USE_TABLE_NAME, LAST_FUEL_UP_DATE_CLAUSE,
REGISTRATION_NO_COLUMN + "=? ", new String[]{registrationNumber},
null, null, null);
其中 LAST_FUEL_UP_DATE_CLAUSE
是 MAX(date_time)
,其中 date_time
是相关列的名称。
当表中有条目时,这工作正常,但在编写单元测试时,当表中没有条目时,我希望游标的行数为零,但我得到的是值为零的一行。 IE。 date_time
列中的最大值为零,而实际上没有任何值。
我很乐意围绕这个进行编码(使用 0
表示没有记录而不是 -1
),但我想知道这是否是预期的行为,或者我是不是做错了什么。
最佳答案
这似乎是预期的行为。
参见 SQLite 文档 here
具体说:
max(X)
The max() aggregate function returns the maximum value of all values in the group. The maximum value is the value that would be returned last in an ORDER BY on the same column. Aggregate max() returns NULL if and only if there are no non-NULL values in the group.
关于android - Android 上的 SQLite MAX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137899/