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.
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.
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!
*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.
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.
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 + "'";
Code: String query; query = "SELECT * FROM skillz WHERE player = '" + p.toString() + "';"; ResultSet res = db.sqlQuery(query); And if
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.
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
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 + "!"); }
Hate to ask this but did you initialize the variable? Code: manageMySQL = new mysqlCore(log, logPrefix, host, database, username, password); manageMySQL.initialize();
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*