SQL reads only one row

Discussion in 'Plugin Development' started by oneill011990, Feb 20, 2015.

Thread Status:
Not open for further replies.
  1. Hi,
    I am reading data from a SQLite databse and use this code to do so (The important part at least)

    Code:
    ResultSet rs = db.query("SELECT * From FastTravelSigns");
    
            while (rs.next()) {
                String name = rs.getString(1);
                UUID creator = UUID.fromString(rs.getString(2));
                World signloc_World = plugin.getServer().getWorld(rs.getString(3));
                int signloc_X = rs.getInt(4);
                int signloc_Y = rs.getInt(5);
                int signloc_Z = rs.getInt(6);
                float signloc_Yaw = rs.getFloat(7);
                World tploc_World = plugin.getServer().getWorld(rs.getString(8));
                int tploc_X = rs.getInt(9);
                int tploc_Y = rs.getInt(10);
                int tploc_Z = rs.getInt(11);
                float tploc_Yaw = rs.getFloat(12);
                boolean automatic = db.parseBoolean(rs.getInt(13));
                float price = rs.getFloat(14);
                int range = rs.getInt(15);
    
                List<UUID> players = null;
    
                players = db.getList(rs.getBytes(16));
    
                if (!players.contains(creator)){
                    players.add(creator);
                }
    
                Location tpLoc = null;
                Location signLoc = null;
    
                signLoc = new Location(signloc_World, signloc_X, signloc_Y, signloc_Z);
                signLoc.setYaw(signloc_Yaw);
    
                tpLoc = new Location(tploc_World, tploc_X, tploc_Y, tploc_Z);
                tpLoc.setYaw(tploc_Yaw);
    
                FastTravelSignDB.addSign(new FastTravelSign(name, creator, price, signLoc, tpLoc, automatic, range,
                        players));
    
            }
    
            rs.close();
    While the plugin knows that there are x rows to check it only reads the first one.
    I have no idea why this is happening. This is the first time I work with SQL and this should work as far as I learnd.

    Hope you can help me
    oneill011990
     
  2. Offline

    candyfloss20

    @oneill011990
    Are there any error(s) in the console?

    *PS : Not sure if it would help but this is how my plugin loops over all columns*
    Code:
        public static Integer getNewID() {
            Main.getAPI().makeConnection();
            try {
                PreparedStatement statement = connection.prepareStatement("SELECT `ID` FROM `userData`");
                ResultSet result = statement.executeQuery();
                while (result.next()) {
                    if (result.last()) {
                        return result.getInt("ID") + 1;
                    }
                }
                return -1;
            } catch (Exception e) {
                e.printStackTrace();
            }
            return -1;
        }
     
  3. For this one row erverything works as it should. After the first one it just stops. No exceptions or errors.
    I do this to calculate the number of rows in the db and load them. Almost the same as above just the fully method here:

    Code:
    public static void load() throws SQLException, IOException {
            db.init();
    
            entries = db.query("SELECT COUNT(*) FROM FastTravelSigns").getInt(1);
    
            if (entries == 0){
                plugin.getLogger().info("No signs found in the database");
                return;
            } else {
                plugin.getLogger().info(entries + " FastTravelSigns found in the database. Starting to load them.");
            }
    
            ResultSet rs = db.query("SELECT * From FastTravelSigns");
    
            while (rs.next()) {
                String name = rs.getString(1);
                UUID creator = UUID.fromString(rs.getString(2));
                World signloc_World = plugin.getServer().getWorld(rs.getString(3));
                int signloc_X = rs.getInt(4);
                int signloc_Y = rs.getInt(5);
                int signloc_Z = rs.getInt(6);
                float signloc_Yaw = rs.getFloat(7);
                World tploc_World = plugin.getServer().getWorld(rs.getString(8));
                int tploc_X = rs.getInt(9);
                int tploc_Y = rs.getInt(10);
                int tploc_Z = rs.getInt(11);
                float tploc_Yaw = rs.getFloat(12);
                boolean automatic = db.parseBoolean(rs.getInt(13));
                float price = rs.getFloat(14);
                int range = rs.getInt(15);
    
                List<UUID> players = null;
    
                players = db.getList(rs.getBytes(16));
    
                if (!players.contains(creator)){
                    players.add(creator);
                }
    
                Location tpLoc = null;
                Location signLoc = null;
    
                signLoc = new Location(signloc_World, signloc_X, signloc_Y, signloc_Z);
                signLoc.setYaw(signloc_Yaw);
    
                tpLoc = new Location(tploc_World, tploc_X, tploc_Y, tploc_Z);
                tpLoc.setYaw(tploc_Yaw);
    
                FastTravelSignDB.addSign(new FastTravelSign(name, creator, price, signLoc, tpLoc, automatic, range,
                        players));
    
            }
    
            rs.close();
            plugin.getLogger().info("Loaded " + FastTravelSignDB.getAllSigns().size() + " FastTravelSigns.");
        }
     
Thread Status:
Not open for further replies.

Share This Page