[CLOSED] Creating Mysql tables with custom rows

Discussion in 'Spigot Plugin Development' started by mlgcraftnetwork, May 20, 2016.

  1. I started learning how to use Mysql databases today but I'm having trouble. I'm trying to create a table called "coins" with two rows, UUID and coins but I'm having trouble. I spent 3 hours looking on the internet to get this to work but i was not able to get it to work. My code is creating the coins database but the rows are not working and it added the row names as colums. Could anyone show me an example mysql command to create a table with two custom named rows? Thanks!

    Code I have now:
    Code (Text):
       public static void createTable() throws SQLException {
       Main.c.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS coins (UUID varchar(200), coins int);");
       }
     
  2. @mlgcraftnetwork
    • Why is the method static, it likely shouldn't be.
    • UUID is a reserved keyword, so either pick a different column name or wrap it in `backticks`
    • UUIDs will always have 36 characters, not 200... (Also, you should use CHAR because the data has a constant length)
     
    • Like Like x 2
    • Agree Agree x 1
  3. Also, you don't name rows, you name columns
     
  4. I tried to change the things you suggested but I'm still having trouble. Also, its possible to name your rows right? Pretty sure its possible. Also, the void is static because I was getting errors in my main class and eclipse suggested me to add that and it fixed it (I'm stil learning java so please don't mind my way of coding unless thats what is causing the problem).
    My database:
    [​IMG]
    Example of a db with custom rows:
    [​IMG]
    If you know how I can do this please share a simple code example, I like to learn things myself and I'm not asking you to spoonfeed me everything but a code example would help me understand this much faster. Thanks!
     
  5. All i see in your example is a table (not a database) with the columns db, name, type, specific_name, language, sql_data_access, is_deterministic and secutory_type
    The second row is just data in this table.

    Naming columns is expected behaviour.

    Example create statement:

    Code (Text):
    CREATE TABLE IF NOT EXISTS `coins` (
      `UUID` char(36) NOT NULL DEFAULT '',
      `coins` int(11) NOT NULL DEFAULT '0',
      PRIMARY KEY (`UUID`)
    );
     
  6. Serializator

    Supporter

    The question is also, why are you creating a separate table to store coins?

    Sent from my SM-A510F using Tapatalk
     
  7. I'm probably making a lot of mistakes because I started learning mysql today, I basicly want to store the amount of coins a player has to a mysql database. Does anyone know how to do that?

    This is my coins class:
    Code (Text):
    package mcgglobal;

    import java.sql.ResultSet;
    import java.sql.SQLException;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.Plugin;

    public abstract class Coins implements Plugin {

       public static void createTable() throws SQLException {
       Main.c.createStatement().executeUpdate("CREATE TABLE IF NOT EXISTS `coins` (`UUID` char(36) NOT NULL DEFAULT '',`coins` int(11) NOT NULL DEFAULT '0',PRIMARY KEY (`UUID`));");
       }

       public static void defaultBal(Player p) throws SQLException {
       ResultSet res = Main.c.createStatement().executeQuery("SELECT * FROM coins WHERE UUID = '" + p.getUniqueId() + "';");
       res.next();
       if(res.getString("UUID") == null) {
       Main.c.createStatement().executeUpdate("INSERT INTO coins (`UUID`, `coins`) VALUES ('" + p.getUniqueId() + "', '0');");
       }
       }
    }
    This is what the database looks like with your code:
    [​IMG]
     
  8. Serializator

    Supporter

    These are in my opinion not questions that should be asked on Spigot, you should learn the basics of MySQL without involving any other progamming languages, I personally really like w3schools.

    Sent from my SM-A510F using Tapatalk
     
  9. I have been looking at the mysql section on w3schools a lot today but I have not found out how to get this to work.
     
  10. Serializator

    Supporter

    If you had you would know how to do it, because the things you need to know for your problem are at the beginning of the tutorial...
     
  11. I have read the w3schools tutorial on how to create tables, if you don't believe me thats fine. Thanks for your suggestion anyways.
     
  12. Serializator

    Supporter

    You said you've been looking at the w3schools SQL section a lot today, not saying you've only specifically looked at a single section.
    And as I said, learn SQL. With learning I mean the whole concept of SQL and how to use it, not sections of it to solve a single problem at a time.
     
    • Agree Agree x 1
  13. Okay, will do that.