title: How do I prepopulate my Room database?
body: <p>It is my first time experimenting with Room database and I want to pre populate the database with 20 User objects. But the code I'm using adds 20 more users to the existing list everytime it runs. How do I keep it from doing that?</p>
  
  <pre><code>@Database(entities = {User.class}, version = 1)
  public abstract class AppDatabase extends RoomDatabase {
  
  private static AppDatabase INSTANCE;
  public abstract DataExchange dataExchange();
  
  public static AppDatabase getAppDatabase(final Context context){
      if (INSTANCE == null){
          INSTANCE = Room.databaseBuilder(context,
                  AppDatabase.class,
                  "User_Database")
                  .addCallback(new Callback() {
                      @Override
                      public void onCreate(@NonNull final SupportSQLiteDatabase db) {
                          super.onCreate(db);
                          Executors.newSingleThreadScheduledExecutor().execute(new Runnable() {
                              @Override
                              public void run() {
                                  getAppDatabase(context).dataExchange().insert(User.populate());
                              }
                          });
                      }
                  })
                  .build();
      }
      return INSTANCE;
  }
  
  public static void destroyInstance(){
      INSTANCE = null;
  }
  </code></pre>
  
  <p>Here's the User class:</p>
  
  <pre><code>@Entity(tableName = "user_table")
  public class User{
  
  public User(String name){
      setName(name);
  }
  
  @PrimaryKey(autoGenerate = true)
  private int uid;
  
  @ColumnInfo(name = "Username")
  private String name;
  
  public String getName() {
      return name;
  }
  
  public void setUid(int uid) {
      this.uid = uid;
  }
  
  public void setName(String name) {
      this.name = name;
  }
  
  public static User[] populate(){
      User[] ar = new User[20];
      for(int i=0; i&lt;20;i++){
          ar[i] = new User("USER "+(i+1));
      }
      return ar;
  }
  }
  </code></pre>
  
  <p>Also, where I took the code from had a getInstance(context) instead of getAppDatabase(context) inside run(). I changed it because getInstance was found in 5 libraries I didn't know about.</p>
  
  <p>Here's DataExchange :</p>
  
  <pre><code>@Dao
  public interface DataExchange {
  
  @Query("SELECT * FROM user_table")
  List&lt;User&gt; getAll();
  
  @Query("SELECT * FROM user_table where username LIKE  :name ")
  User findByName(String name);
  
  @Query("SELECT COUNT(*) from user_table")
  int countUsers();
  
  @Update
  void update(User user);
  
  @Insert
  void insertAll(User... users);
  
  @Delete
  void delete(User user);
  
  }
  </code></pre>