java - SQLite 'BETWEEN' 的行为不符合预期

标签 java sqlite jdbc

我正在尝试选择位于两列值之间的所有行。这些值是 UNIX 时间戳,但查询根本不返回任何行。为什么会这样?

这是我的代码:

public static ArrayList<Appointment> getAppointments(long start, long end) {
    ArrayList<Appointment> appointments = new ArrayList<>();
    try {
        Statement stat = db.createStatement();
        System.out.println("\n" + start + "  ->  " + end);

        // debugging only
        try (ResultSet rs = stat.executeQuery("SELECT * FROM appointments;")) {
            while (rs.next()) {
                long time = rs.getLong("date");
                if (time >= start && time <= end) {
                    System.out.println("Should be adding to list");
                }
            }
        } // 

        try (ResultSet rs = stat.executeQuery("SELECT * FROM appointments WHERE date BETWEEN " + start + " AND " + end + ";")) {
            while (rs.next()) {
                System.out.println("ADDING");
                appointments.add(new Appointment(rs.getLong("date"), rs.getInt("duration")));
            }
        }
    } catch (SQLException ex) {
    }
    System.out.println(appointments);
    return appointments;
}

表中三行的值如下:

1347390000000 15
1347461100000 30
1347469200000 45

输出是:

date = 1347390000000 duration = 15
date = 1347461100000 duration = 30
date = 1347469200000 duration = 45

1350141222283  ->  1350746022283
[]

1349536422283  ->  1350141222283
[]

1348931622283  ->  1349536422283
[]

1348330422283  ->  1348931622283
[]

1347725622283  ->  1348330422283
[]

1347120822283  ->  1347725622283
Should be adding to list
Should be adding to list
Should be adding to list
[]

1346516022283  ->  1347120822283
[]

最佳答案

您必须声明列的数据类型。

关于java - SQLite 'BETWEEN' 的行为不符合预期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13033357/

相关文章:

java - 以 utf8 格式接收的字符串无法正确显示

java - 在 Java 中准备 SQL 查询时如何设置整数列表

sql - SQL WHERE具有多个条件:快捷方式?

android - 比较 Android sqlite 数据库中的日期(存储为字符串)

java - 如何设置 JDBCDriverLogging 的日志记录级别

java - 从 Java 中的两个方法抽象代码,也许使用委托(delegate)?

java - 打开下载的 SQLite 数据库

java - Android SQLite 应用程序中没有这样的表(有 2 个表)

java - 仅在不处于自动提交模式时回滚并提交

java.sql.SQLException : Access denied for user 'userx' @'%' (using password: YES)