ORMLite 查询日期

发布于 2024-12-04 22:58:42 字数 1080 浏览 1 评论 0原文

我正在尝试查询低值和高值之间的日期:

Car car = new Car(1, "octavia");
car.setManufactured(Calendar.getInstance().getTime());
Dao<Car, Integer> carDao = getHelper().getCarDao();

carDao.create(car);

QueryBuilder<Car, Integer> carQb = carDao.queryBuilder();

Calendar yesteday = Calendar.getInstance();
yesteday.add(Calendar.DATE, -1);

Calendar tommorrow = Calendar.getInstance();
yesteday.add(Calendar.DATE, 1);

carQb.where().between("manufactured", yesteday.getTime(), tommorrow.getTime());
PreparedQuery<Car> query = carQb.prepare();

List<Car> cars = carDao.query(query);

Log.d(TAG, cars.get(0).toString());

汽车对象看起来是这样的:

public class Car {

    @DatabaseField(id = true)
    private int id;
    @DatabaseField
    String name;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    User user;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private Brand brand;
    @DatabaseField(columnName="manufactured")
    private Date manufactured;
}

不幸的是,我没有得到任何结果。哪里可能有问题?

I am trying to query for a date between low and hign value:

Car car = new Car(1, "octavia");
car.setManufactured(Calendar.getInstance().getTime());
Dao<Car, Integer> carDao = getHelper().getCarDao();

carDao.create(car);

QueryBuilder<Car, Integer> carQb = carDao.queryBuilder();

Calendar yesteday = Calendar.getInstance();
yesteday.add(Calendar.DATE, -1);

Calendar tommorrow = Calendar.getInstance();
yesteday.add(Calendar.DATE, 1);

carQb.where().between("manufactured", yesteday.getTime(), tommorrow.getTime());
PreparedQuery<Car> query = carQb.prepare();

List<Car> cars = carDao.query(query);

Log.d(TAG, cars.get(0).toString());

Car object looks this way:

public class Car {

    @DatabaseField(id = true)
    private int id;
    @DatabaseField
    String name;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    User user;
    @DatabaseField(foreign = true, foreignAutoRefresh = true)
    private Brand brand;
    @DatabaseField(columnName="manufactured")
    private Date manufactured;
}

Unfortunaly I am getting no results. Where could be a problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

遇见了你 2024-12-11 22:58:42

这是你的问题:你做了一个剪切&粘贴错误。你将明天设置为等于今天,然后无意中将昨天设置为明天。没有哪一天既大于明天又小于今天,因此您不会再获得任何行。

将您的代码更改为如下所示:

Calendar tommorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);  // change yesterday to tomorrow

Here's your problem: you made a cut & paste error. You set tomorrow equal to today, then inadvertently set yesterday equal to tomorrow. There are no days that are both greater than tomorrow and less than today, so you got no rows back.

Change your code to look like this:

Calendar tommorrow = Calendar.getInstance();
tomorrow.add(Calendar.DATE, 1);  // change yesterday to tomorrow
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文