SQLite DELETE查询不会从数据库中删除

时间:2018-06-18 05:40:57

标签: java sqlite

这是我的addStudent代码:

@FXML
private void addStudent(ActionEvent event) {

    // sql query to insert data into students at ID, first name, last name, email and DOB
    String sqlInsert = "INSERT INTO students(id,fname,lname,email,DOB) VALUES (?,?,?,?,?)";

    try {

        Connection conn = dbConnection.getConnection();
        PreparedStatement stmt = conn.prepareStatement(sqlInsert);

        // add the data in the right column
        stmt.setString(1, this.id.getText());
        stmt.setString(2, this.firstname.getText());
        stmt.setString(3, this.lastname.getText());
        stmt.setString(4, this.email.getText());
        stmt.setString(5, this.dob.getEditor().getText());

        stmt.execute();
        conn.close();

    } catch(SQLException ex) {
        ex.printStackTrace();
    }

}

这是我的removeStudent代码:

@FXML
private void removeStudent(ActionEvent event) {

    try {

        // sql query to delete data from the database
        String sqlRemove = "DELETE FROM students WHERE id = ?";

        // open a connection to the database and use PreparedStatement to 
        // initialize the query.
        Connection conn = dbConnection.getConnection();
        PreparedStatement delete = conn.prepareStatement(sqlRemove);

        // information needed to delete the row
        delete.setString(1, selectStudent());

        // execute and delete
        delete.executeUpdate();

        // close the connection
        conn.close();

    } catch (SQLException ex) {
        ex.printStackTrace();
    }

    // update table after deleting
    loadStudentData(event);

}

enter image description here

上图是我桌子的视图。我点击LoadData并显示我的表值。我希望能够点击一行(学生)并点击删除学生将其删除。

removeStudent的辅助方法:

    private String selectStudent() {

String result = "";

try {

        String sqlSelect = "SELECT id FROM students";

        Connection conn = dbConnection.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sqlSelect);

        result = rs.getString(1);
        conn.close();

    } catch (SQLException ex) {

        ex.printStackTrace();
    }

    return result;

}

我很确定它与我在行上“点击”时有关,其中的id值没有被保存在任何地方,所以当我点击“删除”时,没有给它任何删除。

我不知道。任何建议都会很棒。 :d

首先编辑:没有为delete.setString(1,this.id.getText())分配任何内容。当我点击该行并点击删除时,没有任何事情发生,因为当我点击该行时,没有任何内容被分配给id。但是,当我实际给它一个要删除的ID时,查询字符串可以正常工作。还验证按钮确实有效;它用一个很好的'System.out.println(“expletive”)为我打印出一条可爱的信息;

第二次编辑:好的,所以我更新了removeStudent代码,现在我得到的是返回的字符串“null”。什么都没有删除。什么都没有更新。什么都没发生,除了我在控制台中得到“null”。

第三次编辑:越来越近了!由于意识到removeStudent没有被删除ID,我决定创建一个私有帮助器方法来执行SELECT查询。现在,当我点击删除时,它会删除....但是从顶部开始,而不是在我希望它被选中的位置。代码在上面。

第四次编辑:越来越近了!所以,我想出了如何捕获我在表中单击的行,我可以删除......但是,由于我的sqlRemove命令,我正在删除id,所以如果我点击索引为3的行,然后只有表中的id为3的行将被删除,没有别的。我必须重新编写sqlRemove命令的措辞。

1 个答案:

答案 0 :(得分:0)

我修好了它:

    private String selectStudent() {

    // initial value for result to return
    String result = "";

    // grab the index of the row selected on the table
    int initial = studenttable.getSelectionModel().getSelectedIndex();

    try {

        // SELECT query to execute
        String sqlSelect = "SELECT id FROM students";

        Connection conn = dbConnection.getConnection();
        ResultSet rs = conn.createStatement().executeQuery(sqlSelect);

        // while there's a next row
        while(rs.next()) {

            // set temp to equal the id rs.next() is currently on
            String temp = rs.getString("id");
            // get the row id - 1 since we start at 0
            int temp1 = rs.getRow() - 1;

            // if temp1 is equal to the index we selected
            if(temp1 == initial) {

                // make it equal to result
                result = temp;
            }
        }

        // close the connection
        conn.close();

    } catch (SQLException ex) {

        ex.printStackTrace();
    }

    // return the row to delete
    return result;
}

评论中发生了什么。我终于想出了如何从选定的行传递值并将其与行进行比较。一旦我得到正确的行,我就把它交给删除函数删除。

经过一天的一半.............但我喜欢它,所以。呀。

相关问题