title: SQLite - How to first insert then update rows inside the database?
body: <blockquote>
  <p><strong>Question:</strong> What's the best way to insert values into a database then update values?</p>
  </blockquote>
  <p>I'm relatively new to SQLite, but I'm having trouble changing the values of a row in my database. I've copied and pasted the whole class below:</p>
  <pre><code>public class Database {
  
      private final GalaxyCore plugin;
      private Connection connection;
  
      public Database(GalaxyCore plugin) {
          this.plugin = plugin;
      }
  
      @Nullable
      public Connection getConnection() {
          File dataFolder = new File(this.plugin.getDataFolder(), &quot;database.db&quot;);
          if (!dataFolder.exists()) {
              try {
                  if (dataFolder.createNewFile()) {
                      this.plugin.getLogger().info(StringUtil.PREFIX + ChatColor.GREEN + &quot; Successfully created new database folder for storing player information.&quot;);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
  
          try {
              if (this.connection != null &amp;&amp; !this.connection.isClosed()) {
                  return this.connection;
              }
  
              Class.forName(&quot;org.sqlite.JDBC&quot;);
              this.connection = DriverManager.getConnection(&quot;jdbc:sqlite:&quot; + dataFolder);
              this.connection.setAutoCommit(true);
              return this.connection;
          } catch (ClassNotFoundException | SQLException e) {
              this.plugin.getLogger().log(Level.SEVERE, &quot;Failed to retrieve database connection&quot;, e);
          }
  
          return null;
      }
  
      public void loadDatabase() {
          this.connection = this.getConnection();
          try {
              if (this.connection != null) {
                  Statement createTableStatement = this.connection.createStatement();
                  createTableStatement.executeUpdate(&quot;CREATE TABLE IF NOT EXISTS userdata (&quot;
                          + &quot;unique_id MESSAGE_TEXT NOT NULL,&quot;
                          + &quot;receive_alerts INTEGER,&quot;
                          + &quot;receive_pings INTEGER,&quot;
                          + &quot;last_spawn_hat MESSAGE_TEXT NOT NULL&quot;
                          + &quot;);&quot;);
                  createTableStatement.close();
              }
          } catch (SQLException e) {
              this.plugin.getLogger().log(Level.SEVERE, &quot;Failed to load information from database&quot;, e);
          }
      }
  
      @Nullable
      public PlayerData readPlayerData(Player player) {
          UUID playerUUID = player.getUniqueId();
          Connection playerConnection;
          PreparedStatement statement = null;
          ResultSet result = null;
          try {
              playerConnection = this.getConnection();
              if (playerConnection != null) {
                  statement = playerConnection.prepareStatement(&quot;SELECT * FROM userdata WHERE unique_id = '&quot; + playerUUID + &quot;';&quot;);
                  result = statement.executeQuery();
                  if (result.getString(&quot;unique_id&quot;).equals(playerUUID.toString())) {
                      String name = result.getString(&quot;username&quot;);
                      int alerts = result.getInt(&quot;receive_alerts&quot;);
                      boolean receiveAlerts = result.getInt(&quot;receive_alerts&quot;) == 1;
                      boolean receivePings = result.getInt(&quot;receive_pings&quot;) == 1;
                      Material spawnHat = Material.getMaterial(result.getString(&quot;last_spawn_hat&quot;));
  
                      PlayerData playerData = new PlayerData(player);
                      playerData.setReceiveAlerts(receiveAlerts);
                      playerData.setReceivePings(receivePings);
                      playerData.setLastSpawnHat(new ItemStack(spawnHat == null ? Material.GLASS : spawnHat));
                      return playerData;
                  }
              }
          } catch (SQLException e) {
              this.plugin.getLogger().log(Level.SEVERE, &quot;Failed to read player data profile for &quot; + player.getName(), e);
          } finally {
              this.close(statement, result);
          }
  
          return null;
      }
  
      public void writePlayerData(PlayerData playerData) {
          UUID playerUUID = playerData.getUniqueID();
          Connection playerConnection;
          PreparedStatement statement;
          try {
              playerConnection = this.getConnection();
              if (playerConnection != null) {
                  statement = playerConnection.prepareStatement(&quot;INSERT INTO userdata(unique_id, receive_alerts, receive_pings, last_spawn_hat) VALUES(?, ?, ?, ?)&quot;);
                  statement.setString(1, playerUUID.toString());
                  statement.setInt(2, playerData.isReceiveAlerts() ? 1 : 0);
                  statement.setInt(3, playerData.isReceivePings() ? 1 : 0);
                  statement.setString(4, playerData.getLastSpawnHat() == null ? Material.GLASS.toString() : playerData.getLastSpawnHat().getType().toString());
                  statement.executeUpdate();
                  statement.close();
              }
          } catch (SQLException e) {
              this.plugin.getLogger().log(Level.SEVERE, &quot;Failed to write player data profile for &quot; + playerData.getName(), e);
          }
      }
  
      public void close(PreparedStatement statement, ResultSet result) {
          try {
              if (statement != null) {
                  statement.close();
              }
  
              if (result != null) {
                  result.close();
              }
          } catch (SQLException e) {
              this.plugin.getLogger().log(Level.SEVERE, &quot;Failed to close SQLite connection with database&quot;, e);
          }
      }
  }
  </code></pre>
  <p>Essentially, the class creates a database that stores a person's unique ID, two booleans for receiving alerts and pings, and the name of the last item they selected (more or less). When I first start up my program, the database works accordingly. If I toggle the <code>receive_alerts</code> entry, the value gets updated and saved. However, if I toggle it again and try to save, it doesn't override the original value in the database.</p>
  <p>I believe my issue is due to inserting the value, rather than updating the value. However, I'm not sure how to check if a value already exists within a column. I've tried simply google searching, but nothing important comes up.</p>
  <p>I was thinking that I could first check to see if a player's <code>unique_id</code> is already present in column 1. If it does, then <code>UPDATE</code>. If it doesn't, then <code>INSERT</code>. But how would I do that?</p>