Override things in mysql?

Discussion in 'Plugin Development' started by jusjus112, Dec 1, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    jusjus112

    Hey guys,

    Im coding a plugin for mysql, everything works!
    But when i set the line with this:
    Code:java
    1. statement.executeUpdate("INSERT INTO Coins (`PlayerName`, `Coins`) VALUES ('" + name + "', '0');");

    I am setting the information in the table, but i want to override that line in the table.
    So if a player has won 1 coin, they have behind his name a 1!
     
  2. Offline

    bgsteiner

    After creating the row of information in the table you need to update the table not insert. Insert creates a new line
    http://www.w3schools.com/sql/sql_update.asp

    Code:
    statement.executeUpdate("UPDATE Coins SET Coins=" + value + " WHERE PlayerName='" + playerName + "';");
    
     
  3. Offline

    jusjus112

    bgsteiner
    Nice, thank you (Solved)

    bgsteiner
    Another question?
    Can i use this if the line did not exist and create him?
    Code:
    "INSERT INTO Coins IF NOT EXISTS (`PlayerName`, `Coins`) VALUES ('" + name + "', '0');"
    Or, how i create a index/row if it not exist?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 29, 2016
  4. Offline

    fireblast709

    jusjus112 make the PlayerName column unique, and use ON DUPLICATE KEY UPDATE.
     
  5. Offline

    jusjus112

    fireblast709
    Ehm, i have tried putting the "ON DUPLICATE KEY UPDATE"
    But he cant find it, or he dont undersand what it is!
     
  6. Offline

    mythbusterma

    jusjus112

    "he" being who, exactly? I can assure that MySQL will interpret a column declared as "ON DUPLICATE KEY UPDATE" correctly.
     
    werter318 likes this.
  7. Offline

    fireblast709

  8. Offline

    jusjus112

    fireblast709 mythbusterma
    Yea, i know. But something i dont get.
    When i try this:
    Code:java
    1. statement.executeUpdate("INSERT INTO Coins (`PlayerName`, `Coins`) VALUES ('" + name + "', '0') ON DUPLICATE KEY UPDATE Coins=Coins+1;");

    It creates a row! But when the player joins twice, there are standing 2 row/lines!
    How to override the line/row that already exist?
     
  9. Offline

    fireblast709

  10. Offline

    jusjus112

    @fireblast709
    Please help me, i cant get this working. im so confused right now!
    How can i make it unique? And i dont understand the link you have send!
    Im happy with a code thats explane me the code?
     
  11. Offline

    fireblast709

    @jusjus112 You will have to learn SQL if you want to use SQL :p.
     
Thread Status:
Not open for further replies.

Share This Page