java SQL 错误或缺少数据库(“?”附近:语法错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27041163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 11:06:31  来源:igfitidea点击:

SQL error or missing database (near "?": syntax error)

javasqlite

提问by Tomasz Mlynarczyk

Would you be that kind and tell me whats wrong in here? connis DriverManager.getConnection(DB_URL)

你会那么好心告诉我这里有什么问题吗?connDriverManager.getConnection(DB_URL)

        try {
            PreparedStatement prepStmt = conn.prepareStatement(
                    "UPDATE week SET ?=? WHERE id>=? AND id<=?");
            prepStmt.setString(1, s);
            prepStmt.setFloat(2, x);
            prepStmt.setInt(3, c);
            prepStmt.setInt(4, d);
            prepStmt.executeUpdate();
        } catch(SQLException e) {
            System.err.println("Error during data update");
            e.printStackTrace();
        }

Error is in first line of "try" and it goes like "SQL error or missing database (near "?": syntax error)". I have to add that when I put this statement in cmd with "?" substituted with values it works as charm.

错误在“try”的第一行,类似于“SQL 错误或丢失的数据库(靠近“?”:语法错误)”。当我将此语句放在带有“?”的 cmd 中时,我必须补充一点。用价值代替它就像魅力一样。

回答by Eran

You can't pass column names as parameters to the prepared statement. You can only pass values as parameters :

您不能将列名作为参数传递给准备好的语句。您只能将值作为参数传递:

    try {
        PreparedStatement prepStmt = conn.prepareStatement(
                "UPDATE week SET some_column_name=? WHERE id>=? AND id<=?");
        prepStmt.setFloat(1, x);
        prepStmt.setInt(2, c);
        prepStmt.setInt(3, d);
        prepStmt.executeUpdate();
    } catch(SQLException e) {
        System.err.println("Error during data update");
        e.printStackTrace();
    }

回答by brlaranjeira

You cannot pass a column name to the PreparedStatement. What you could do to overcome this is change it in the string.

您不能将列名传递给PreparedStatement. 你可以做些什么来克服这个问题是在字符串中改变它。

try {
    PreparedStatement prepStmt = conn.prepareStatement(
    "UPDATE week SET " + s + " =? WHERE id>=? AND id<=?");
    prepStmt.setFloat(1, x);
    prepStmt.setInt(2, c);
    prepStmt.setInt(3, d);
    prepStmt.executeUpdate();
} catch(SQLException e) {
    System.err.println("Error during data update");
    e.printStackTrace();
}

回答by MiladiuM

As it was mentioned you can not pass column namesor sql syntaxto the PreparedStatement. however you can use String.format()to pass anything to your sql:

如前所述,您不能将列名sql 语法传递给PreparedStatement. 但是你可以使用String.format()任何东西传递给你的 sql:

try {
                String sql = "UPDATE week SET %s=? WHERE id>=? AND id<=?";
                sql = String.format(sql , "s")
                PreparedStatement prepStmt = conn.prepareStatement(sql);
                prepStmt.setFloat(1, x);
                prepStmt.setInt(2, c);
                prepStmt.setInt(3, d);
                prepStmt.executeUpdate();
            } catch(SQLException e) {
                System.err.println("Error during data update");
                e.printStackTrace();
            }

sa you can see ? = ?is replaced with %s = ?and later the column name was replaced with String.formatand later the String was passed to the PreparedStatement. however this should not be used pass data because it defies the purpose of PreparedStatements.

您可以看到 sa? = ?被替换为%s = ?,后来列名被替换为String.format,后来字符串被传递给PreparedStatement. 但是这不应该用于传递数据,因为它违背了PreparedStatements的目的。

回答by brso05

You should not do ? = ? you should specify the field id = ?...or whatever field you want there.

你不应该吗?= ? 您应该指定字段 id = ?... 或您想要的任何字段。

PreparedStatement prepStmt = conn.prepareStatement(
                    "UPDATE week SET columnName=? WHERE id>=? AND id<=?");