jdbc:从sqlite数据库获取日期

时间:2018-03-21 12:07:18

标签: java sqlite jdbc

我已将sqlite数据库中的日期保存为unixepoch值,并且在尝试获取日期时我收到错误:'没有这样的列存在'日期'“。

以下是我创建表格的方法:

public void createActivityTable()   {
    Statement stmt = null;

    try (Connection connection = connectionClass.connectToMainDb(true)) {
        stmt = connection.createStatement();
        String table = "CREATE TABLE IF NOT EXISTS EMPLOYEE_ACTIVITY (" +
                "ActivityIndex INTEGER PRIMARY KEY AUTOINCREMENT," +
                "employeeID INTEGER NOT NULL," +
                "Date INTEGER NOT NULL," +
                "PCName TEXT NOT NULL," +
                "Amount REAL," +
                "Activity TEXT NOT NULL," +
                "FOREIGN KEY (employeeID) REFERENCES EMPLOYEES(employeeID) ON DELETE NO ACTION ON UPDATE CASCADE," +
                "FOREIGN KEY (PCName) REFERENCES COMPUTERAVERAGE(PCName) ON DELETE NO ACTION ON UPDATE CASCADE" +
                ");";
        stmt.executeUpdate(table);

        stmt.close();
        connection.close();
    } catch (SQLException e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
        System.exit(0);
    }
}

这是插入方法:

public void insertProductivity(int employeeID, double amount, String activityType, String pieceType)    {
    Statement stmt = null;

    try (Connection connection = connectionClass.connectToMainDb(true)) {
        stmt = connection.createStatement();
        String query = "INSERT INTO EMPLOYEE_ACTIVITY (employeeID, Date, PCName, Amount, Activity, PieceType) " +
                "VALUES ('" + employeeID + "', strftime('%s', 'now'), '" + User.getLoggedPC().replaceAll("[^a-zA-Z0-9]", "") +
                "', '" + amount + "', '" + activityType + "', '" + pieceType + "');";
        stmt.executeUpdate(query);

        stmt.close();
        connection.close();
    } catch (SQLException e)    {
        e.printStackTrace();
    }
}

以下是我尝试获取rs.next while循环中获取异常的日期的方式:

public void getStats()    {
    PreparedStatement stmt = null;

    try (Connection connection = connectionClass.connectToMainDb(false))    {
        String query = "SELECT datetime(Date, 'unixepoch') FROM EMPLOYEE_ACTIVITY;";
        stmt = connection.prepareStatement(query);
        ResultSet rs = stmt.executeQuery();


        while (rs.next())   {
            System.out.println(rs.getInt("Date"));
        }

        rs.close();
        stmt.close();
        connection.close();
    } catch (SQLException e)    {
        e.printStackTrace();
    }
}

我得到了下一个错误:

java.sql.SQLException: no such column: 'Date'
at org.sqlite.jdbc3.JDBC3ResultSet.findColumn(JDBC3ResultSet.java:48)
at org.sqlite.jdbc3.JDBC3ResultSet.getInt(JDBC3ResultSet.java:401)
at employee.stats.StatsHandler.getStatsByYear(StatsHandler.java:24)
at gui.Main.start(Main.java:20)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:748)

1 个答案:

答案 0 :(得分:1)

您的select语句指定SELECT datetime(Date, 'unixepoch'),因此结果集中确实没有名为Date的列。您可能希望为列命名,例如:

SELECT datetime(Date, 'unixepoch') AS d

然后获取列d