SQLite'BETWEEN'的行为与预期不符

时间:2012-10-23 14:57:52

标签: 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
[]

1 个答案:

答案 0 :(得分:0)

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