Records from my android – orm sqlite db
id, name, value, date/time
------------------------------------
1 John 23 05/17/24 2:20 AM
2 Paul 26 05/17/24 5:25 AM
3 Mathew 27 05/17/24 5:28 PM
4 Sara 23 05/17/24 5:30 PM
As per the code below, I can retrieve all the records as a List<Record>
.
In this code I am passing start, end
parameter in getRecordsBetween()
function as today’s date (17-5-2024
).
Now, how can I get only the last record/last record inserted in the database for today i.e, (4, Sara, 23, 05/17/24 5:30 PM
) to return as a List<Record>
Note : DateTime start, DateTime end
filter condition is only for today/single day
.
Also am using Joda - DateTime
feature.
RecordDao.java
import android.util.Log;
import androidx.annotation.NonNull;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.SelectArg;
import org.joda.time.DateTime;
import org.joda.time.DateTimeConstants;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
...
public List<Record> getRecordsBetween(DateTime start, DateTime end) {
if (start == null || end == null) {
return new ArrayList<>();
}
start = start.withTimeAtStartOfDay();
end = end.withTime(DateTimeConstants.HOURS_PER_DAY - 1,
DateTimeConstants.MINUTES_PER_HOUR - 1,
DateTimeConstants.SECONDS_PER_MINUTE - 1,
DateTimeConstants.MILLIS_PER_SECOND - 1);
try {
return getQueryBuilder()
.orderBy(Record.Column.DATE, true)
.where().gt(Record.Column.DATE, start)
.and().lt(Record.Column.DATE, end)
.query();
} catch (SQLException exception) {
Log.e(TAG, exception.toString());
return new ArrayList<>();
}
}
...
[1] Trying/No success
–
I tried to get the first record using queryForFirst()
but it required to return as a Record
because it’s a single record not a List<Record>
return join(Record.class).orderBy(Record.Column.DATE, false)
.where().le(Record.Column.DATE, DateTime.now()).queryForFirst();
[2] Another lookup step
–
If I filtered out or clear all the records from the List<Record>
except the last one then I can get my desired result.
entriesOfDay.subList(0, entriesOfDay.size()-1).clear();
I want to use queryForFirst()
but no luck. Please suggest me on this.