【问题标题】:Having trouble deleting a row from a table in SQL在 SQL 中从表中删除行时遇到问题
【发布时间】:2017-09-24 02:35:27
【问题描述】:

我无法从 SQL 表中删除一行。架构的名称是“imageusers”。在“imageusers”中有一个名为“images”的表,它包含三列:imageID、username 和 image,但我将忽略 image 列,因为我可以删除该行而无需为该信息制作准备好的语句。

我的代码:

myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/imageusers?autoReconnect=true&useSSL=false&relaxAutoCommit=true", "user", "password");

String update =  ("DELETE FROM images WHERE imageID = ?, username = ?");


PreparedStatement preparedStatement = myConn.prepareStatement(update);
preparedStatement.setString(1, "image name");
preparedStatement.setString(2, "username");

preparedStatement.executeUpdate();

我已经仔细检查了所有内容。 myConn 连接有效。图像名称和用户名字符串有效且存在于 sql 表中。

但是当我尝试运行它时,我收到以下错误:

"com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: 你有 SQL 语法错误;检查与您对应的手册 MySQL 服务器版本,用于在 'WHERE imageID = 附近使用正确的语法 '图像名称',用户名 = '用户名'' 在第 1 行。

我在网上看了,这是在 JDBC 中从 sql 中删除一行的正确方法,不是吗?我看到的每个例子都是这样的。我正在运行 MySQL 工作台。

【问题讨论】:

  • 您需要在 where 子句中使用 'and' 而不是逗号: Where imageID = ?和用户名 = ?

标签: java mysql jdbc


【解决方案1】:

你的语法是关闭的,这个

String update =  ("DELETE FROM images WHERE imageID = ?, username = ?");

应该使用AND 而不是, - 喜欢

String update =  "DELETE FROM images WHERE imageID = ? AND username = ?";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    相关资源
    最近更新 更多