Solved How to loop through all the rows in a certain table? via mysql

Discussion in 'Plugin Development' started by Eos, Aug 18, 2015.

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

    Eos

    How would I be able to loop through all the rows in a certain table to see if it contains a specific value or string and do something?
     
    Last edited: Aug 18, 2015
  2. Offline

    AdobeGFX

    My strongest side.. mysql.. (I suck at it..)
    but i googled, and found this..
    Code:
    "SELECT * FROM sometable"
    Get's every data inside the table, i think/hope/belive/pray for
     
  3. Offline

    Eos

    I know that, my problem is looping through all the players uuid in the mysql database.
     
  4. Offline

    pie_flavor

    @Eos If you use the EbeanServer included with Bukkit, i believe you can do EbeanServer#find(Class).findSet()
     
  5. Offline

    Epicballzy

    Use ResultSet#next() in a while loop.
     
  6. Offline

    dreamermike1

    This'll help if you're using SQL queries in a database to get a specific response. If you're not, please ignore;

    When you're looking for something specific within a table, then you just need to tell your SQL query to look for something specific.

    "SELECT * FROM tablename" gives you everything in the table.

    "SELECT * FROM tablename WHERE uuidcolumn = playeridyouwant" filters the table to provide a list of results that match your criteria (the value in uuidcolumn equalling playeridyouwant)
     
  7. Offline

    Eos

    @dreamermike1
    Well how could I change all the players value if it contains something specific?
     
  8. @Eos
    Code:
    ResultSet set = query("SELECT * FROM `Something`");
    while(set.next()) { //It's like an iterator, but this is Iterator#hasNext() and Iterator#next() in one
      String someValue = set.getString("Key"); //Instead of using Iterator#next() to get a value, the ResultSet now acts as the element (row)
      if(someValue.contains("somethingSpecific")) {
        //Do stuff
      }
    }
     
  9. Offline

    RainoBoy97

    You don't need to loop through all the rows to find what you need, the server does that.
    Code:
    SELECT * FROM some_table WHERE some_column = some_value
    will give you all the rows where some_column is equal to some_key
     
    Konato_K likes this.
  10. Offline

    Hawktasard

    @Eos
    I don't quite get what you're trying to do, if you're trying to change a specific column for every single row that has x as y then you can do something like this:
    Code:php
    1. UPDATE my_table SET coins=0 WHERE coins=150 AND somethingElse=7

    (I'm pretty sure this is correct. Don't count on it though)
     
    Konato_K likes this.
  11. Offline

    Eos

    dreamermike1 likes this.
Thread Status:
Not open for further replies.

Share This Page