尽管查询在其他地方工作,但Sqlite PreparedStatement不返回ResultSet

时间:2019-01-28 13:34:47

标签: java sqlite jdbc

我遇到一个问题,我收到一个空的ResultSet用于我知道可用于给定数据库的查询。

此数据库(被截断和匿名化)如下:

-- Table: places
CREATE TABLE places (id INTEGER PRIMARY KEY AUTOINCREMENT, venue_id INTEGER, timestamp INTEGER, venue_name TEXT);
INSERT INTO places (id, venue_id, timestamp, venue_name) VALUES (1, 1, 1533231909000, 'Place 1');
INSERT INTO places (id, venue_id, timestamp, venue_name) VALUES (2, 2, 1533231909000, 'Place 2');
INSERT INTO places (id, venue_id, timestamp, venue_name) VALUES (3, 1, 1533231911000, 'Place 1');
INSERT INTO places (id, venue_id, timestamp, venue_name) VALUES (4, 1, 1533232901300, 'Place 1');
INSERT INTO places (id, venue_id, timestamp, venue_name) VALUES (5, 2, 1533233301300, 'Place 2');
INSERT INTO places (id, venue_id, timestamp, venue_name) VALUES (6, 3, 1533245000000, 'Place 3');
-- Table: opening_times
CREATE TABLE opening_times (id INTEGER PRIMARY KEY AUTOINCREMENT, venue_id INTEGER, timestamp INTEGER);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (1, 1, 1546300836000);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (2, 2, 1546300840000);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (3, 1, 1546300840000);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (4, 1, 1533231911000);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (5, 1, 1533232011000);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (6, 3, 1533232011000);
INSERT INTO opening_times (id, venue_id, timestamp) VALUES (7, 2, 1533232011000);

我的代码正在生成以下SQL命令(尽管打印效果不佳)

SELECT DISTINCT p.venue_name,
                q.changes
FROM   (SELECT COUNT(*) - 1 AS changes,
               venue_id,
               timestamp
        FROM   opening_times t
        GROUP  BY t.venue_id) q
       JOIN places p
         ON q.venue_id = p.venue_id
WHERE  q.changes > 0
       AND q.timestamp >= 0
       AND q.timestamp < 1633232011000
ORDER  BY q.changes DESC,
          p.venue_name ASC; 

在SQLite数据库编辑器程序(也在this SQLFiddle中执行)中使用此查询可返回正确的结果,例如

| venue_name | changes |
|------------|---------|
|    Place 1 |       3 |
|    Place 2 |       1 |

但是,我的Java代码对于相同数据库没有得到相同的结果。

Class.forName("org.sqlite.JDBC");
Connection connection = DriverManager.getConnection(url);
connection.setAutoCommit(true);

String sql = "..."; // See above
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet resultSet = statement.executeQuery();

while(resultSet.next()) {
   // Code here never executes at present as nothing is returned in the result set
}

我已经准备了许多SO帖子,据我所知,为了正确获取数据,我遵循了正确的步骤。其他不太复杂的查询也可以正常工作,似乎这个特定查询目前无法返回数据。

如果这有什么区别,我还将使用Java 8和org.xerial:sqlite-jdbc 3.25.2版连接到数据库。

1 个答案:

答案 0 :(得分:1)

此问题已通过恢复为<div class="page"> <div class="content">Content here</div> <div class="footer">Footer</div> </div>

来解决。

我在OSX 10.14机器上遇到了此问题,但是更高版本的3.25.2似乎在Windows和Linux上都适用于其他用户。