简体   繁体   中英

MySQL Error [1064] in Updating a Database

At the moment I am writing a small program which should check whether a file is in a database and if its SHA264 is correct. If it is not correct it should be updated in the database.

The function which I have written for updating are the following:

public static void updateSHA(String TABLE, String shaOLD, String shaNEW)
{
    boolean success = false; 

    Connection con = null;
    Statement statement = null;
    ResultSet resultSet = null;
    PreparedStatement prepStatement = null;
    String update = "SET SQL_SAFE_UPDATES = 0" + "; \n" // if I remove this line resp. "'SET SQL_SAFE_UPDATES = 0' + '; \n' + " I get another error code, see below
            + "UPDATE " + dbName + "." + TABLE + " SET sha256 = '" + shaNEW + "' WHERE sha256 = '" + shaOLD + "'" + ";";

    try
    {
        con = DBConnection();

        con.prepareStatement(update);
        statement = con.createStatement();
        resultSet = statement.executeQuery(update);

        con.commit();
        System.out.println("Successfully updated " + shaOLD + " to " + shaNEW + " in " + dbName + "." + TABLE);

        success = true;
    }
    catch (SQLException sqle)
    {
        System.out.println(update);
        System.out.println("Error at updateSHA for " + shaOLD + ": " + "[" + sqle.getErrorCode() + "] " + sqle.getMessage());
    }
    finally
    {
        if (con != null)
        {
            try
            {
                con.close();
            }
            catch (SQLException sqle)
            {
                System.out.println("Error at updateSHA for " + shaOLD + " while closing con: " + "[" + sqle.getErrorCode() + "] " + sqle.getMessage());
            }
        }

        if (statement != null)
        {
            try
            {
                statement.close();
            }
            catch (SQLException sqle)
            {
                System.out.println("Error at updateSHA for " + shaOLD + " while closing statement: " + "[" + sqle.getErrorCode() + "] " + sqle.getMessage());
            }
        }

        if (!success)
        {
            System.exit(-1);
        }
    }
}

An example of an update query:

SET SQL_SAFE_UPDATES = 0; 
UPDATE databaseName.tableName SET sha256 = '4e89f735c019ab1af439ec6aa23b85b66f2be1a1b15401b2471599d145cfda42' WHERE sha256 = '000c675c73567f4256c7de7ad7c43056d5f002e08c1a5c7b4ff77a4dd8e479a3';

The exact error is:

Error at updateSHA for 000c675c73567f4256c7de7ad7c43056d5f002e08c1a5c7b4ff77a4dd8e479a3: [1064] You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE databaseName.tableName SET sha256 = '4e89f735c019ab1af439ec6' at line 2

I think it has something to do with the "new" sha which should replace the old one (because in the error it looks like the sha gets cut off or something like that).

If I copy and paste the above update query in MySQL Workbench I do not get any errors and everything works fine (the entry gets updated).

If I delete the line 'SET SQL_SAFE_UPDATES = 0" + "; \\n' I get the following error code:

Error at updateSHA for 000c675c73567f4256c7de7ad7c43056d5f002e08c1a5c7b4ff77a4dd8e479a3: [0] Can not issue data manipulation statements with executeQuery().

An assumption is, that the second error occurs because of the MySQL safe update mode.

So my question: Why do I get the 1064 error reps. why does the new sha get cut?

You are trying to execute two statements in one statement execute. This is not allowed by JDBC. Technically the MySQL JDBC driver has a connection property to allow this, but it is disabled by default (as the behavior violates the JDBC spec). You need to split it into two queries. Note however that the default for SQL_SAFE_UPDATES already is 0.

See allowMultiQueries in Driver/Datasource Class Names, URL Syntax and Configuration Properties for Connector/J if you really need to execute two queries as one statement.

The second part of your problem (after removing the SET SQL_SAFE_UPDATES = 0; ) is that you are trying to use executeQuery to execute a statement that does not produce a ResultSet . That is not allowed. You should use executeUpdate (or execute ) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM