这是一行
PreparedStatement delete = con.prepareStatement("DELETE FROM " + tableName +" WHERE first LIKE " + firstName + " AND last LIKE " + lastName);
在删除行之前,我想检查名字和姓氏是否都与查询匹配。 “第一”和“最后”都是我表中的列。 我收到此错误消息
org.sqlite.SQLiteException: [SQLITE_ERROR] SQL error or missing database (no such column: Hendrix)
在这种情况下,Hendrix是lastName
的值。
在这种情况下,为什么最后一个类似____无效?如果我正在检查,是否应该使用另一个关键字 多个字符串?
答案 0 :(得分:0)
In Java, you can escape quotes with \:
PreparedStatement delete = con.prepareStatement("DELETE FROM " + tableName + " WHERE first = \"" + firstName + "\" AND last = \"" + lastName + "\"");
当然,我觉得有点难以理解,因此我更喜欢混合使用单引号和双引号而不是将其转义:
PreparedStatement delete = con.prepareStatement('DELETE FROM ' + tableName + ' WHERE first = "' + firstName + '" AND last = "' + lastName + '"');
关于您对LIKE的疑问。 Try reading up on the difference between LIKE and =。那里有很多信息。简而言之... LIKE与通配符一起使用。
答案 1 :(得分:0)
如果您按预期的方式使用准备好的语句,并使用占位符作为值,然后将这些值与setString
绑定,则不会出现此问题。尝试以下方法:
PreparedStatement delete = con.prepareStatement("DELETE FROM " + tableName +" WHERE first LIKE ? AND last LIKE ?");
delete.setString(1, "%" + firstName + "%");
delete.setString(2, "%" + lastName + "%");
delete.executeUpdate();
请注意,要使用LIKE
,需要在绑定的值中包含%
。如果您想比较精确的值,则可以使用以下方法:
PreparedStatement delete = con.prepareStatement("DELETE FROM " + tableName +" WHERE first = ? AND last = ?");
delete.setString(1, firstName);
delete.setString(2, lastName);
delete.executeUpdate();