[INACTIVE] SQLite and MySQL Tutorial/Library

Discussion in 'Resources' started by alta189, May 12, 2011.

  1. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Is this an appropriate way to check if a field exists? I've never used this before so I'm kinda iffy on using it.
    Code:
    
        public boolean checkField(String table, String column) {
            DatabaseMetaData dbm;
            boolean exists = false;
            try {
                dbm = connection.getMetaData();
                ResultSet columns = dbm.getColumns(null, null, table, column);
                while(columns.next()){
                    String columnName = columns.getString("COLUMN_NAME");
                    if(columnName.equals(column)){
                        exists = true;
                        break;
                    }
                }
            } catch (SQLException se) {
                core.writeError("Failed to check if column \"" + column + "\" exists: " + se.getMessage(), true);
                exists = false;
            }
            return exists;
        }
    EDIT: Added the variable 'column' in "dbm.getColumns" into one of the parameters. Found an example online and it seems that this would work fine.
  2. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    I have a different setup then his release but I'll try and get those parts together for you. :p
  3. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hm it seems that I have changed it a bit too much to the lib so I can only give you a little.
    Code:
    dbCore = new SQLCore(TLogger.getLog(), TLogger.getPrefix(),
        Template.name, plugin.getDataFolder().getPath() + "/Data");
    dbCore.initialize();
    
    Thats really not much lol.
    After that I create the table.

    EDIT: Oh TLogger.log is just the minecraft logger.
    EDIT: and Template.name is the name of my plugin.
  4. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    My problem is that my update is not working. This is the query I'm using. "UPDATE 'players' SET 'awesomeness' = '1' WHERE 'playername' = 'Tagette'"

    EDIT: Found it! Stupid me... It should not have single quote around table and field names... GRR

    SO: " UPDATE players SET awesomeness = '1' WHERE playername = 'Tagette' " is correct!
  5. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    *FIX*
    In your databaseHandler I found that your updateQuery function is using 'statement.executeQuery'. It should be using 'statement.executeUpdate', and I also changed that in the insertQuery function. Havn't looked at the insert though, however it works fine for me.

    EDIT: The deleteQuery also uses 'statement.executeQuery', I changed that as well. The 'statement.executeQuery' returns a ResultSet so the Update, Insert, and Delete don't use that one.
  6. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Thnx for the replies, i will check it out when i get behind my pc
  7. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    already knew that, I had sent you a message saying the version I sent you had that error.
  8. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    alright =P

    EDIT: You didn't say that specific error, just that you had errors.
  9. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Ok, I found out the problem. Appearently, the handler doesn't like a new database in the main plugin folder or something. I got errors when trying to create one in plugins/Skillz, but not in plugins/Skillz + "/data"
    Which is weird. Also, this is correct right?:
    Code:
    String query;
                query = "SELECT id, player, skill, xp, level FROM skillz WHERE player = " + p;
                ResultSet res = db.sqlQuery(query);
    Because it gives me errors.
  10. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    The "/data" works fine for me. It shouldn't give an error when in 'plugins/Skillz' though.
    Try this:
    Code:
    query = "SELECT id, player, skill, xp, level FROM skillz WHERE player = '" + p + "'";
  11. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Code:
    String query;
                query = "SELECT * FROM skillz WHERE player = '" + p.toString() + "';";
                ResultSet res = db.sqlQuery(query);
    And if
  12. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Close but not perfect :D
  13. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    This is weird..
    Code:
    String query;
    String p = event.getPlayer().getName().toString();
    query = "SELECT * FROM skillz WHERE player = '" + p + "'";
                ResultSet res = db.sqlQuery(query);
                if(res == null){
    and I get this:
    Code:
    java.lang.NullPointerException
            at nl.lolmen.Skillz.SkillzPlayerListener.onPlayerJoin(SkillzPlayerListener.java:54)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:243)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:58)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:310)
            at net.minecraft.server.ServerConfigurationManager.c(ServerConfigurationManager.java:117)
            at net.minecraft.server.NetLoginHandler.b(NetLoginHandler.java:96)
            at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:74)
            at net.minecraft.server.Packet1Login.a(SourceFile:43)
            at net.minecraft.server.NetworkManager.b(NetworkManager.java:215)
            at net.minecraft.server.NetLoginHandler.a(NetLoginHandler.java:40)
            at net.minecraft.server.NetworkListenThread.a(SourceFile:91)
            at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:396)
            at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:308)
            at net.minecraft.server.ThreadServerApplication.run(SourceFile:422)
    I want to use this to insert some data only once, when there is no data of that player found yet.
  14. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Send me your source again
  15. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Will do tomorrow, in bed now xD
  16. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    OK, we will figure out this issue :D
  17. Offline

    Jayjay110

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    OH poo I just finished my plugin, everything works fine for sql but it wont create the mysql database or table? help:

    2011-05-29 16:53:54 [SEVERE] Error occurred while enabling MysteryBox v1.0 (Is it up to date?): null
    java.lang.NullPointerException
    at com.alta189.sqlLibrary.MySQL.DatabaseHandler.checkTable(DatabaseHandler.java:134)
    at com.alta189.sqlLibrary.MySQL.mysqlCore.checkTable(mysqlCore.java:72)
    at com.servegame.n1p.MysteryBox.MysteryBox.loadMySQL(MysteryBox.java:399)
    at com.servegame.n1p.MysteryBox.MysteryBox.onEnable(MysteryBox.java:66)
    at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:125)
    at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:750)
    at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:253)
    at org.bukkit.craftbukkit.CraftServer.loadPlugin(CraftServer.java:132)
    at org.bukkit.craftbukkit.CraftServer.loadPlugins(CraftServer.java:110)
    at org.bukkit.craftbukkit.CraftServer.reload(CraftServer.java:337)
    at org.bukkit.command.SimpleCommandMap$ReloadCommand.execute(SimpleCommandMap.java:243)
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:127)
    at org.bukkit.craftbukkit.CraftServer.dispatchCommand(CraftServer.java:271)
    at net.minecraft.server.MinecraftServer.b(MinecraftServer.java:401)
    at net.minecraft.server.MinecraftServer.h(MinecraftServer.java:386)
    at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:292)
    at net.minecraft.server.ThreadServerApplication.run(SourceFile:394)



    At line 399

    if (this.manageMySQL.checkTable("blocks")){

    help :(
  18. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    This works fine, the SQL Is starting and stuff:
    Code:
    if(useSql == true && useMySQL == false){
                log.info(logPrefix + "SQLite warming up...");
                new File(maindir + "data").mkdir();
                dbManager = new sqlCore(this.log, this.logPrefix, "skillz", "plugins/Skillz/data");
                dbManager.initialize();
                if (dbManager.checkTable("skillz").booleanValue() == false) {
                    String query = "CREATE TABLE skillz (id int, player TEXT, skill TEXT, xp int, level int);";
                    dbManager.createTable(query);
                }
            }
    But then we have this:
    Code:
    public void onPlayerJoin(PlayerJoinEvent event){
            String p = event.getPlayer().getName().toString();
            }else if(useSql == true && useMySQL == false){
                String query;
                query = "SELECT * FROM skillz WHERE player = '" + p + "'";
                ResultSet res = db.sqlQuery(query);
                if(res == null){
                    //There is no field for the player yet.
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", acrobatics , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", archery , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", digging , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", swords , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", woodcutting , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", unarmed , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", axes , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", swimming , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", farming , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", archery , 0 , 0";
                    db.sqlQuery(query);
                    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", mining , 0 , 0";
                    db.sqlQuery(query);
                    Skillz.log.info("Skillz SQL Entry created for " + p  + "!");
                    
                }
    
  19. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Hate to ask this but did you initialize the variable?

    Code:
    manageMySQL = new mysqlCore(log, logPrefix, host, database, username, password);
    manageMySQL.initialize();
  20. Offline

    Tagette

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    It seems your missing something in each of the querys.

    This:
    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", mining , 0 , 0";

    Should be:
    query = "INSERT INTO skillz (player, skill, xp, level) values (" + p + ", mining , 0 , 0)";

    *note the missing parentheses at the end*
  21. Offline

    Jayjay110

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    yeah I did lol
  22. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    oh lol, forgot a ). Should I add a ; in there aswell?
  23. Offline

    Jayjay110

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    can you see anything wrong wit mine?
  24. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    try to put it in another folder, it worked for me.
  25. Offline

    Jayjay110

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    really?

    EDIT:
    Didnt work, its in /data, no change
  26. Online

    Lolmewn BukkitDev Staff

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Then I have no clue.
  27. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    MySQL database already has to be made, sorry haven't found a way for it to make the database yet :D
  28. Offline

    totokaka

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    how can i check if a player is added to the database? so i dont add him twice?
  29. Offline

    alta189

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)
    Use a select query. w3schools.com
  30. Offline

    totokaka

    dev.bukkit.org profile:
    CFUSERNAME
    My Plugins (CFCOUNT)

Share This Page