java - SQLite DELETE 查询不会从数据库中删除
问题描述
这是我的 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);
}
上图是我桌子的视图。我点击 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 命令的措辞方式。
解决方案
我修好了它:
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;
}
正在发生的事情在评论中。我终于想出了如何从选定的行中传递值并将其与一行进行比较。一旦我得到正确的行通过,我就把它交给删除函数来删除。
半天后......但我喜欢它,所以。是的。
推荐阅读
- reactjs - ReactJS SetTimeout / setInterval
- algorithm - 建议将一个数字写为三个数字的总和,其中每个数字至少为 2
- css - 如何修复从“无处”出现的滚动条的这种自我向上滚动效果?
- node.js - 如何使用聚合 mongoDB 从 2 个集合中获取链接数据
- java - 我可以像这样将带有 Map 的 3 个点放入方法参数中吗: private Map addAllMood(Map...map)
- javascript - Cheerio 加载字符串而不替换内部脚本内容
- node.js - 天蓝色事件的相关ID
- python - 二进制数的语法格式
- docker - Filecoin Textileio Powergate 没有足够的矿工来自声誉模块来满足约束错误
- github - GitHub 归档过时的文件