SQLite? MySQL? Help?

Discussion in 'Plugin Development' started by icomputertinker, Nov 22, 2013.

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

    icomputertinker

    Hey there,
    I've got a plugin under development, and I think I might need some database action going on. The only problem is, I've never used databases before. I have a rough idea of what they're about, but I don't know how to use them. Could someone please give me some links to some Java-and-databases tutorials or other pointers on which database types/how to use them?

    Thanks so much, I know you might be thinking: "Just google it" but trust me, posting here is my last resort.
     
  2. Offline

    tommycake50

    Look on youtube for an SQL tutorial.
    I think thebcbroz has a good one and possibly other people who do bukkit tutorials too.
     
  3. Offline

    1Rogue


    bcbroz are very unprofessional and teach bad habits.

    @OP

    What have you tried so far?
     
  4. Offline

    tommycake50

    Yeah, thats true.
    I learned from them though and my knowledge was set straight by these forums :D.
    But they have a nice teaching style, even if they use somewhat controversial methods.
     
  5. Offline

    Blah1

    1Rogue when are you going to start making tutorials? I'm gonna subscribe lol
     
  6. Offline

    1Rogue


    When I have a functional computer again.
     
  7. Offline

    Wahsu

    @icomputertinker Here you go, this helped me out a lot. idk how you are with knowing java programming, but I knew a good amount before i saw this video.
     
  8. Offline

    icomputertinker

  9. Offline

    Maulss

    Sorry for the late reply.
    Yes, that's where I started to learn from, but that tutorial isn't the best idea if you're concerned about resource usage.

    It's fine if you learn from it but I don't recommend you using that directly in your plugin. Instead try making an Asynchronous timer that sends the information from hashmaps to your database per interval.
     
  10. Offline

    icomputertinker

    Maulss After looking at the thread a little, I realized I had no idea what he was talking about. I'm going to need to do some research on MySQL and SQLite before any of that makes sense to me.

    By the way, what do you mean when you say that the tutorial isn't good if you're concerned about resource usage? Do you mean memory, or processor, or other?

    Thanks.
     
  11. Offline

    Maulss

    I'm sorry, I was gone for a few days.

    What I was saying earlier was that the tutorial is fine to use for learning purposes. If you use that on a public server which handles transfers between MySQL and your server, it can result in some lag. It all depends how fast your the Minecraft server and MySQL/SQLite server's network speed is. If they're on the same machine is shouldn't cause too much of a problem, but still if database transactions are often used like that it can still make some lag.

    For example, if I send a value to the database it shouldn't be too bad but if I expect to grab something out, the servers needs to complete that return first, and then continue. So if I want to grab a player's kills from the database, the server will stop functioning until the value is returned. This generally takes about 0.5 - 2 seconds if the database is hosted externally.

    icomputertinker
    To prevent the TPS drop while accessing the database, my recommendation is to use multi-threading like I've stated above. Most of the things you do are done on the main thread. A single core can only handle one task a time (Correct me if I'm wrong), so while it grabs stuff out of the database the server freezes and waits until that task is complete. Running tasks in other threads (Asynchronously) will run the task in the background. So the server will continue functioning as intended, hopefully with no TPS drops, and at the same time the plugin will be accessing the database in the background.

    I can show you how to run Asynchronous tasks if you want.
     
  12. Offline

    icomputertinker

    Maulss
    Hey there, sorry I was gone for a couple weeks. Vacation. Anyways, could you please show me how to run Asynchronous tasks? I've never needed to do that before, and I think it'd be useful.
     
  13. Offline

    Maulss

    There are numerous ways, but I'll touch on easiest and quickest way to do it.

    Let's say you want to set a player's kills to 10.

    Assuming you already have the database created and connected, we create a 'send update' method, like this:
    Code:java
    1.  
    2. Plugin plugin; // plugin instance inherited from your main class. Warning: Must not be null.
    3. Statement statement; // More about statements on the tutorial above. Warning: Must not be null.
    4.  
    5. public void sendUpdate(final String table, final String player, final String column, final String value) {
    6. // Run your task asynchronously:
    7. Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() {
    8. public void run() {
    9. // Send your player's kills to the database.
    10. statement.executeUpdate("UPDATE `"+table+"` SET "+column+"='"+value+"' WHERE player='"+player+"");
    11. }
    12. });
    13. }

    Then you call your method like this:
    Code:java
    1. String table = "playerKills";
    2. String playerName = player.getName();
    3.  
    4. sendUpdate(table, playerName, "kills", "10");

    Warning: Don't use the asynchronous tasks on working with the Bukkit API.

    Source: http://jd.bukkit.org/rb/apidocs/org/bukkit/scheduler/BukkitRunnable.html

    EDIT:
    If you want to send data per interval, I'd recommend making a custom class for that:

    Firstly, in your onEnable(), add:
    Code:java
    1. long interval;
    2.  
    3. new newClass().runTaskTimerAsynchronously(this, 0L, interval);


    And in your 'newClass' class, use this:

    Code:java
    1. public class newClass extends BukkitRunnable {
    2.  
    3. public void run() {
    4. // your code here
    5. }
    6.  
    7. }
     
  14. Offline

    bgsteiner

  15. Offline

    icomputertinker

    Maulss
    Awesome! Thank you so much for this.
     
  16. Offline

    Maulss

    Glad to help!
     
Thread Status:
Not open for further replies.

Share This Page