SQL (Does Value Exist)

Discussion in 'Spigot Plugin Development' started by Jonnyo101, Jun 4, 2017.

  1. Hello. I am trying to figure out if a "value" in the sql database.
    With the primary key being IdKey


    https://gyazo.com/1de8db08b6cc3b442c49615e50499cf2
    Example of database.

    How can i check if "9dcac4d7-8554-3d81-a516-19802307d130" is in the database.
     
  2. electronicboy

    IRC Staff

    do a select on the uuid, and check how many rows you get from the operation, if you get more than 0 rows, it exists in the database
     
  3. SELECT COUNT(column_uuid) WHERE column_uuid=?

    set(1, 9dcac4d7-8554-3d81-a516-19802307d130)
     
  4. Simple:

    PreparedStatement st = connection.prepareStatement("SELECT IdKey FROM LgPlayerData WHERE IdKey = ?");
    st.setString(1, "9dcac4d7-8554-3d81-a516-19802307d130");
    ResultSet rs = st.executeQuery();

    if (rs.next()) {
    //contains
    } else {
    // not contains
    }
     
  5. Note that you should not use this to determine whether you need to insert a new row, or to update an old one. You'd use INSERT INTO ON DUPLICATE KEY UPDATE for that.
     
    • Agree Agree x 1