[LIB] SQLibrary - Database wrappers for all database engines

Discussion in 'Resources' started by PatPeter, Aug 26, 2011.

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

    PatPeter

    XenForo gave me no notification on any messages in this thread for the past page.

    Anyone have any immediate issues?
     
  2. Offline

    Neodork

    Still fine, still only using the MySQL part, I don't know about the SQLlite users.
     
  3. Offline

    ProjectInfinity

    Well, I have a minor issue with SQLite using a previous version of your library. I am a little reluctant to update as things are working out pretty smoothly so far.

    However, I decided to change my table structure, and since ALTER TABLE in SQLite is so limited, I decided to go with a simple rename, creation, insertion and dropping. My issue is however, the database locks every single time I do any altering changes. SELECTs are fine.

    I currently run the following query when I drop the old table (which also results in a db lock):
    Code:
    db.getConnection().createStatement().executeUpdate("DROP TABLE reportrts_request")
    I read about the issue with SQLite and .query() so I decided to do it like shown above.

    It might be an issue I have caused myself with the startup of my plugin. If you have no suggestions to what I should look for, I suppose I could commit what I am trying and push it to my github.
     
  4. Offline

    kireevm

    You do not understand how to make sure that he wrote to the database to utf-8. but not in cp1251
     
  5. Offline

    HaydenCappadona

    There was a few errors in the example plugin I fixed them all but for the life of me I cant seem to work out how to fix this using the new event system...Any help? ;D

    Code:
            // Register Listeners \\
            PluginManager pm = this.getServer().getPluginManager();
            pm.registerEvent(Event.PLAYER_INTERACT, new OwnerPlayerListener(this), Event.Priority.Normal, this);
     
  6. Offline

    Neodork

    Well first off the example plugin (in the 1st post) is outdated badly.

    And you can replace
    Code:java
    1. pm.registerEvent(Event.PLAYER_INTERACT, new OwnerPlayerListener(this), Event.Priority.Normal, this);

    with:
    Code:java
    1. pm.registerEvents(OwnerPlayerListener,this);
     
    HaydenCappadona likes this.
  7. Offline

    HaydenCappadona

    For some reason OwnerPlayerListener is throwing me a unknown variable error.

    Also, Do you think someone could create a new simple example plugin? Including how to use MySQLite aswell. That would be very helpful to allot of people I think :D
     
  8. Offline

    Neodork


    Yes someone could create an example plugin. but I personally am very busy right now, and I do MySQL not SQLite.
     
  9. Offline

    Jake111119

    Do it like this:
    Code:
    OwnerPlayerListener ownerPlayerListener = new OwnerPlayerListener(this);
    pm.registerEvents(OwnerPlayerListener, this);
     
  10. Offline

    Spikes

    Hello PatPeter!
    I want to Update datas in the MYSQL database and iam getting this error.
    On my Windows Home Server is running fine. On my Linux server i keep getting the error
    if the plugin wants to Update. The script can read out and insert, but not Update
    Code:
    22:31:39 [INFORMATION] <PlayerName> lost connection: disconnect.quitting
    22:31:39 [INFORMATION] Connection reset
    [WARN] Failed to query stty columnsjava.lang.InterruptedException
            at java.lang.Object.wait(Native Method)
            at java.lang.Object.wait(Object.java:503)
            at java.lang.UNIXProcess.waitFor(UNIXProcess.java:210)
            at jline.internal.TerminalLineSettings.exec(TerminalLineSettings.java:196)
            at jline.internal.TerminalLineSettings.exec(TerminalLineSettings.java:170)
            at jline.internal.TerminalLineSettings.stty(TerminalLineSettings.java:165)
            at jline.internal.TerminalLineSettings.get(TerminalLineSettings.java:69)
            at jline.internal.TerminalLineSettings.getProperty(TerminalLineSettings.java:89)
            at jline.UnixTerminal.getWidth(UnixTerminal.java:75)
            at jline.console.ConsoleReader.drawBuffer(ConsoleReader.java:610)
            at jline.console.ConsoleReader.drawBuffer(ConsoleReader.java:629)
            at jline.console.ConsoleReader.drawLine(ConsoleReader.java:357)
            at org.bukkit.craftbukkit.util.TerminalConsoleHandler.flush(TerminalConsoleHandler.java:26)
            at java.util.logging.ConsoleHandler.publish(ConsoleHandler.java:106)
            at java.util.logging.Logger.log(Logger.java:522)
            at java.util.logging.Logger.doLog(Logger.java:543)
            at java.util.logging.Logger.logp(Logger.java:659)
            at org.bukkit.craftbukkit.LoggerOutputStream.flush(LoggerOutputStream.java:27)
            at java.io.PrintStream.write(PrintStream.java:482)
            at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:221)
            at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:291)
            at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:104)
            at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:185)
            at java.io.PrintStream.write(PrintStream.java:527)
            at java.io.PrintStream.print(PrintStream.java:669)
            at java.io.PrintStream.println(PrintStream.java:806)
            at net.minecraft.server.Packet.a(Packet.java:96)
            at net.minecraft.server.NetworkManager.h(NetworkManager.java:149)
            at net.minecraft.server.NetworkManager.c(NetworkManager.java:268)
            at net.minecraft.server.NetworkReaderThread.run(SourceFile:76)
    
    Code:
    MySQL.open();
            String update = "UPDATE  `iconnymc`.`minecraft` SET  `wins` =  '"+ customConfig.getInt("WinnerList." + left.getName() + ".Wins") +"',`points` =  '"+ customConfig.getInt("WinnerList." + left.getName() + ".WinsPoints") +"',`kills` =  '"+ customConfig.getInt("WinnerList." + left.getName() + ".kills") +"',`rounds` =  '"+ customConfig.getInt("WinnerList." + left.getName() + ".Rounds") +"',`score` =  '"+ customConfig.getInt("WinnerList." + left.getName() + ".Rounds") +"' WHERE  user ='"+ left.getName() +"';";
            MySQL.query(update);
            MySQL.close();
     
  11. Offline

    Edzandor

    I am not making a plugin, but i do want to know how i set up a mySQL database on my own computer. i want to use it to show the kills/deaths and be able to rollback my server. I am making a survival games server, using the plugin: http://dev.bukkit.org/server-mods/survival-games/

    And it says i will need a mySQL database, but doesn't show how to make one. I don't want to pay for one online, i want to have it take the files from a spot on my computer... through my own server, or whatever.
     
  12. Offline

    Neodork

    Don't use
    MySQL.open(); and MySQL.close(); every time, just on enable/on disable.

    It seems your query is incorrect try putting it into a MySQL workbench before adding it into your program.
     
  13. Offline

    PatPeter

    I would like to switch the library to a plugin permanently so we don't have over 9000 forks over Bukkitspace, but I have yet to update my own plugin to the new format. If anyone wants to pull request it on Github I'll accept it.
     
  14. Offline

    ProjectInfinity

    That sounds like a bad idea. Keep the source up. What's wrong with forks anyway?
     
  15. Offline

    korikisulda

    What licence, if any, is this library considered to be under?
     
  16. Offline

    gamemakertim

    Can some one make a working tutorial?
    the download link is outdate and i dont get what to do to connect to MySQL
     
  17. Offline

    TomShar

    How could I check is a value isn't in the database?

    Code:
    ResultSet rs = mysql.query("Select * FROM FFA WHERE playername = '" + p.getName() + "'");
    How can I check if that returns nothing aka that name is not in the database?
     
  18. Offline

    xDrapor

    App
    I was wondering the same thing. Apparently if its null it will return '0', maybe we have to catch that?
     
  19. Offline

    PatPeter

    Forks are extremely messy for a library. It'd be fine if someone was forking to make a change and use a pull request to merge it back, but I hate seeing old broken versions of the library strewn about.

    Whatever license Bukkit uses.

    Read the code samples on page 1, 2, and Neodork's code on pages 7 and 8. I could swear I added a link on the first post.

    rs.next();

    Seriously, I remember replying to this post.

    Above should work. Check its return.
     
  20. Offline

    xDrapor

    Yep, before I saw that I googled it, thanks anyway! Love your lib, no issues so far, gonna integrate it soon (Some people don't install the required dependencies, and it might be hard to find this, and I wouldn't wan't to download it when my plugin is launched, either)
     
  21. Offline

    korikisulda

    ah, okay, if it's LGPL then I can include it in my GPL plugin ;) thanks.
     
  22. Offline

    Clavus

    I have a problem. My database is located on another machine than the one minecraft is running on. Sometimes the connection between these two machines fails and a few packets disappear into oblivion. Problem is that my whole Minecraft server freezes until the SQL finally decides there's no connection.

    Can I avoid these connection failures from blocking the main server thread somehow?

    EDIT: rewrote my plugin so all the database calls are made on an asynchronous thread, with the results being pushed back to the main thread. Hopefully fixes my issues.
     
  23. Offline

    Vampist

    Neodork
    Hey I attempted your little scripts on page seven.
    For some reason plugin.mysql throws errors about Nullexception even though I declare it in my class...
    So I changed them to this.mysql now it's hanging at startup.

    It declares mysql then hangs when it enters the try/catch "this.mysql.open();" in sqlconnection

    Any ideas?
     
  24. Offline

    Neodork

    Mind pming me your code or parts of it ? I can't judge from your explanation..
     
  25. Offline

    PatPeter

    Did you make changes to the library itself? If so, please make a pull request:

    https://github.com/PatPeter/SQLibrary/
     
  26. Offline

    dark navi

    Hey PatPeter
    I am having an issue with using the SQLite library. I keep getting this error:

     
  27. Offline

    Dark_Balor

    I would say that the problem is not in that snippet.
    But the code executed before your query.

    More over, to not forgetting the close of the ResultSet, just do a try catch with a finally where you put your
    Code:
     if (rs != null)
                        rs.close();
     
  28. Offline

    dark navi

    I seem to still be getting the error. Here is what I have, assume the player is NOT in the databse when getPlayerThirst is called:

    Code:
    public Integer getPlayerThirst(String playername)
        {   
            ResultSet rs = this.databaseConnection.query("SELECT level FROM thirstlevels WHERE player = '" + playername + "'");
            int level = -1;
            try {
                if(rs.next())
                    level = Integer.getInteger("level");
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } finally {
                if (rs != null)
                    try {
                        rs.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
           
            if(level != -1)
                return level;
            else
                this.setPlayerThirst(playername, 100);
           
            return 100;
        }
       
        public void setPlayerThirst(String playername, int thirstLevel)
        {   
            this.databaseConnection.query("INSERT OR REPLACE INTO thirstlevels (player, level) VALUES ('" + playername + "'," + thirstLevel + ")");
        }
    The code outputs the following:
    Code:
    2012-07-17 12:08:36 [WARNING] [Thirst] [SQLite] Please close your previous Resul
    tSet to run the query:
            INSERT OR REPLACE INTO thirstlevels (player, level) VALUES ('darknavi',1
    00)
    Do I have to close result sets even when I simply query the DB?
     
  29. Offline

    Cowboys1919

    Im having a problem getting this to work.. :/
    Here is my code: (in onEnable)
    Code:
    FileConfiguration config = this.getConfig();
            host    = config.getString("sql.host");
            port    = config.getString("sql.port");
            dbname  = config.getString("sql.dbname");
            user    = config.getString("sql.user");
            password = config.getString("sql.password");
           
            mysql = new MySQL(getLogger(),"MCSpleef", host, port, dbname, user, password);
       
            try {
                this.mysql.open();
            } catch (Exception e) {
                this.getLogger().info(e.getMessage());
                this.getPluginLoader().disablePlugin(this);
            }
    and my config.yml looks like this
    Code:
    sql:
      host: 10.0.0.[local number]
      port: 3306
      dbname: stats
      user: [my user]
      password: [my pass]
    After a good long stall when it says it's enabling my plugin, i get this:
    Code:
    15:13:53 [SEVERE] [Spleefv2] MCSpleef[MySQL] jdbc:mysql://10.0.0.[local number]:3306/stats
    15:13:54 [SEVERE] [Spleefv2] MCSpleef[MySQL] Could not be resolved because of an SQL Exception: Communications link failure
     
    The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server..
    Ideas? Maybe it has to do with connecting over LAN..?

    Btw, PHPMyAdmin over LAN is fine..

    Ok, I suddenly went "FIREWALL, DUH" then excitedly went to allow port 3306, then when i ran it again, i got the same thing just without the big delay. :(
    Why must things not work for me, ugh

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  30. Offline

    Dark_Balor

    If you read the code of the library the query is opening a resultSet everytime. Then yes, you have to get it and close it after each call.

    Moreover : Integer.getInteger("level") is not what you want but rs.getInt("level").
    Else you don't get the result of your query
     
Thread Status:
Not open for further replies.

Share This Page