Solved SQL prints out wrong syntax | I coded it correctly

Discussion in 'Spigot Plugin Help' started by wFw, Jun 25, 2016.

  1. wFw

    wFw

    So here's the code that says it has a wrong syntax:


    else {
    PreparedStatement newPlayer = connection.prepareStatement("INSERT INTO 'player_data' values(?,0,0,1);");
    newPlayer.setString(1, e.getPlayer().getName());
    newPlayer.execute();
    newPlayer.close();
    e.getPlayer().sendMessage(ChatColor.GREEN + "It's the first time you join the server!");

    }

    Here's the error:

    https://www.spigotmc.org/attachments/sqlerror-png.146532/?temp_hash=c3ac66d3361981daf0c77b56976dfe8d

    I'm using Spigot 1.10.1 .

    Please help!
     

    Attached Files:

  2. Don't use ' ' for the table name; use `` for them if anything.

    It's
    Code (Text):
    INSERT INTO player_data(COLUMNS) VALUES(?, '0', '0', '1');
    although you should replace the 0s and 1 with a ? and use setInt on the statement.
     
    #2 Lyxnx, Jun 25, 2016
    Last edited: Jun 25, 2016
    • Like Like x 1
  3. wFw

    wFw

    These are deaths kills and logins.
     
  4. wFw

    wFw

    Thanks a lot!
    I'm such an idiot.. it needed `table` instead of 'table' Thanks a lot dude. You saved my life and my code :p I love you <3
     
  5. wFw

    wFw

    Could you help me make a command with MySQL?

    Check this out:

    int logins;

    if(cmd.getName().equalsIgnoreCase("logins")){
    int logins;
    try {
    PreparedStatement sql = main.connection.prepareStatement("SELECT logins FROM `player_data` WHERE uuid=?;");
    sql.setString(1, p.getUniqueId().toString());
    ResultSet res = sql.executeQuery();
    res.next();

    logins = res.getInt("logins");
    } catch (SQLException e) {
    e.printStackTrace();
    }


    p.sendMessage(ChatColor.GREEN + "You have logged in " + ChatColor.YELLOW + logins + ChatColor.GREEN + " times!");


    }

    logins in the sendmessage method has an error. If i call this method in the try catch Period it does not send the message to the player for some reason.
     
  6. Code (Java):

    if(cmd.getName().equalsIgnoreCase("logins")){
           int logins;
           try {
             PreparedStatement sql = main.connection.prepareStatement("SELECT logins FROM `player_data` WHERE uuid=?;");
             sql.setString(1, p.getUniqueId().toString());
             ResultSet res = sql.executeQuery();
             
             if(res.next()) {
                 logins = res.getInt("logins");
             }
           } catch (SQLException e) {
               e.printStackTrace();
           }
         
           p.sendMessage(ChatColor.GREEN + "You have logged in " + ChatColor.YELLOW + logins + ChatColor.GREEN + " times!");
         }
     
    If ResultSet#next() returns true, there are entries in it otherwise there aren't.
    Also you should do database things asynchronously.
     
    • Like Like x 1
  7. wFw

    wFw


    Thanks man, that really helped. I'm an idiot once again and I also forgot to connect to the MySQL server xP