M
M
Maxim2021-07-09 12:44:32
MySQL
Maxim, 2021-07-09 12:44:32

What is the correct way to specify the SET value?

Good afternoon, I need to implement mysql database UPDATE. It is necessary to specify SET from the type variable, I tried to do it through "?" and "*" , but it doesn't work, then you need to specify the value, help me find the error.

My code:

public void addBlocks(UUID uuid,String type, int blocks){
        try{
            PreparedStatement ps = plugin.SQL.getConnection().prepareStatement("UPDATE PlayerBlocks SET * = ? WHERE UUID=?");
            ps.setString(1,type);
            ps.setInt(2,(getUpgrade(uuid, type) + blocks));
            ps.setString(3, uuid.toString());
            ps.executeUpdate();
        }catch (SQLException e){
            e.printStackTrace();
        }
    }


Throws this error
[13:30:19 WARN]: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''DIRT'=1 WHERE UUID='9c91354c-f13e-303c-97bb-51ea80f5fc3f'' at line 1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Akina, 2021-07-09
@MaximPython

tried to do it through "?" and "*" , but it doesn't work

For. so that the inserted value is not quoted, you should use a placeholder of 2 question marks:
PreparedStatement ps = plugin.SQL.getConnection().prepareStatement("UPDATE PlayerBlocks SET ?? = ? WHERE UUID=?");

Then the first passed parameter is the name of the field to update. 2 and 3 respectively values ​​(new and for the selection condition).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question