[英]SQLite DELETE query won't delete from database
Here's my code for the addStudent:这是我的 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();
}
}
And here's my code for removeStudent:这是我的 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);
}
The picture above is the view of my table.上图是我的桌子的视图。 I hit LoadData and my table values show up.
我点击 LoadData 并显示我的表值。 I want to be able to click on a row(student) and hit Delete Student to remove it.
我希望能够点击一行(学生)并点击删除学生将其删除。
Helper method for removeStudent: 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;
}
I'm pretty sure it has to do with when I "click" on a row, the id value for that isn't being held anywhere so when I hit "Delete" nothing is being given for it to Delete.我很确定这与当我“单击”一行时有关,它的 id 值没有被保存在任何地方,所以当我点击“删除”时,没有任何东西可以删除它。
I don't know.我不知道。 Any advice would be awesome.
任何建议都会很棒。 :D
:D
First edit: nothing is assigned to delete.setString(1, this.id.getText()).第一次编辑:没有分配给 delete.setString(1, this.id.getText())。 When I click on the row and hit delete, nothing is happening because there's nothing being assigned to id when I click on the row.
当我点击该行并点击删除时,什么也没有发生,因为当我点击该行时没有分配给 id 。 The query string DOES work however when I physically give it an ID to delete.
但是,当我实际给它一个要删除的 ID 时,查询字符串确实有效。 Also verified that the button does work;
还验证了按钮是否有效; it prints out a lovely message for me with a good ol' System.out.println("expletive");
它用一个很好的 ol' System.out.println("expletive"); 为我打印出一条可爱的消息;
Second edit: Ok, so I updated the removeStudent code and now all I get is the string "null" returned.第二次编辑:好的,所以我更新了 removeStudent 代码,现在我得到的只是返回的字符串“null”。 Nothing deletes.
什么都不会删除。 Nothing updates.
没有任何更新。 Nothing is happening except I get "null" in the console.
除了我在控制台中得到“null”之外,什么也没有发生。
Third edit: Getting closer!第三次编辑:越来越近! With the realization that the removeStudent isn't being given an ID to delete, I decided to create a private helper method that will do a SELECT query.
意识到没有为 removeStudent 提供要删除的 ID,我决定创建一个私有帮助方法来执行 SELECT 查询。 Now, when I hit delete, it'll delete....but from the top, and not at where I want it selected.
现在,当我点击删除时,它会删除....但是从顶部开始,而不是在我想要选择的位置。 The code is above.
代码在上面。
Fourth edit: Getting even closer!第四次编辑:离得更近了! So, I figured out how to capture the row I click on within the table and I can delete......however, because of my sqlRemove command, I'm deleting by id so if I click on a row with index 3, then ONLY the row within the table that has an id of 3 will be deleted, nothing else.
所以,我想出了如何捕获我在表中点击的行,我可以删除......但是,由于我的 sqlRemove 命令,我将按 id 删除,所以如果我点击索引为 3 的行,那么只有表中 id 为 3 的行会被删除,没有别的。 I gotta re-write how the sqlRemove command is worded.
我必须重新编写 sqlRemove 命令的措辞。
I fixed it:我修好了它:
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;
}
What's going on is in the comments.正在发生的事情在评论中。 I finally figured out how to pass the value from a selected row and compare it to a row.
我终于想出了如何从选定的行传递值并将其与行进行比较。 Once I get the correct row to pass, I give it to the delete function to remove.
一旦我得到正确的行传递,我就把它交给删除函数来删除。
After a day in a half.............but I love it, so.过了半天…………但我喜欢它,所以。 Yeah.
是的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.