SQL multiple set statements

Discussion in 'Plugin Development' started by KingFaris11, Feb 19, 2015.

Thread Status:
Not open for further replies.
  1. Hi, I'm fairly inexperienced with MySQL, so I'm not sure how to have multiple set statements in one prepared statement, if that's possible. I have this:
    Code:
    int gold = 21, silver = 69, copper = 420;
    PreparedStatement goldSql = connection.prepareStatement("UPDATE `coins` SET gold=? WHERE uuid=?;");
    goldSql.setInt(1, gold);
    goldSql.setString(2, player.getUniqueId().toString());
    goldSql.executeUpdate();
    goldSql.close();
    However, my table's going to look like this:
    uuid | gold | silver | copper

    I want to update gold, silver AND copper, and 3 prepared statements for this seems inefficient to me, so I'm pretty sure there's a way of setting multiple keys at once.

    From looking online, I saw someone using commas, so I thought:
    Code:
    UPDATE `coins` SET gold=?,silver=?,copper=? WHERE uuid=?;
    I can't test this as I've not even made the plugin, and this looks a bit fishy since there's no brackets, so can someone tell me if this solution is correct, if not, help me find the solution?
     
    Last edited: Feb 19, 2015
  2. Offline

    Skionz

    @KingFaris11 Test it without making the plugin. It looks fine to me.
     
    KingFaris11 likes this.
  3. Offline

    crolemol

    KingFaris11 likes this.
  4. Thanks, I'll do that now.

    Edit:
    @crolemol
    Ah okay, thanks :D

    Edit 2: Yes, it works, thanks again.
     
    Last edited: Feb 19, 2015
  5. Offline

    1Rogue

    Just a note, something like money or anything that you frequently access you should try to cache locally (via a manager).
     
  6. Oh right, thanks, yeah it'll be accessed very frequently, up to the max number of clicks a player can do per second, so I'll cache it.
     
    Last edited: Feb 19, 2015
  7. Offline

    mythbusterma

    @KingFaris11

    Make sure you interact with the database asynchronously to the server, so as not to bog down the server.
     
    KingFaris11 likes this.
Thread Status:
Not open for further replies.

Share This Page