I am new to Android development and I am trying to develop a very simple app. I want to create a prepopulated database which has one table . The table is about users and it has only three columns id, name , and profession. The only usage of the database will be to search via the name of every user and find their profession. So I only need to prepopulate it with some data.
My issue is that nothing is happening when i run the databse nothing is happening the database is not even been created. cant see anything in the dtabase inspector
As I have read from the documentation of Room database https://developer.android.com/training/data-storage/room/prepopulate I just need to add the following code
Room.databaseBuilder(appContext, AppDatabase.class, "Sample.db")
.createFromAsset("database/myapp.db")
.build();
So I created my small database in SqliteStudio and now I am trying to copy it with the Room database in Android. Bellow you can see a screenshot of my table users_table from sqliteStudio
Dependencies
// Room components
implementation "androidx.room:room-runtime:$rootProject.roomVersion"
implementation 'androidx.wear:wear:1.1.0'
annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"
// Lifecycle components
implementation "androidx.lifecycle:lifecycle-viewmodel:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-livedata:$rootProject.lifecycleVersion"
implementation "androidx.lifecycle:lifecycle-common-java8:$rootProject.lifecycleVersion"
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
compileOnly 'com.google.android.wearable:wearable:2.8.1'
implementation "androidx.lifecycle:lifecycle-extensions:2.2.0"
gradle
ext {
appCompatVersion = '1.3.0'
constraintLayoutVersion = '2.0.4'
coreTestingVersion = '2.1.0'
lifecycleVersion = '2.3.1'
materialVersion = '1.3.0'
roomVersion = '2.3.0'
// testing
junitVersion = '4.13.2'
espressoVersion = '3.1.0'
androidxJunitVersion = '1.1.2'
}
Bellow you can see also the code from my Dao, DatabaseClass, Entity, Repository, ViewModel
User
@Entity(tableName = "users_table")
public class User {
@PrimaryKey(autoGenerate = true)
@NonNull
private int id;
@ColumnInfo(name = "name")
@NonNull
private String name;
@ColumnInfo(name = "profession")
@NonNull
private String profession;
public User(int id, @NonNull String name, @NonNull String profession) {
this.id = id;
this.name = name;
this.profession = profession;
}
all getters and setters also exist
UserDao
@Dao
public interface UserDao {
@Query("SELECT * FROM users_table")
LiveData<List<User>> getAll();
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(User user);
@Delete
void delete(User user);
@Query("DELETE FROM users_table")
void deleteAll();
}
UserDatabase
@Database(entities = {User.class}, version = 1, exportSchema = false)
public abstract class UserDatabase extends RoomDatabase {
public abstract UserDao userDao();
private static volatile UserDatabase INSTANCE;
private static final int NUM_OF_THREADS = 4;
public static final ExecutorService databaseWriteExecutor
= Executors.newFixedThreadPool(NUM_OF_THREADS);
public static UserDatabase getDatabase(final Context context){
if (INSTANCE == null){
synchronized (UserDatabase.class){
if (INSTANCE == null){
INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
UserDatabase.class, "user.db")
.createFromAsset("users.db")
.build();
}
}
}
return INSTANCE;
}
}
UserRepository
public class UserRepository {
private UserDao userDao;
private LiveData<List<User>> allUsers;
public UserRepository(Application application) {
UserDatabase db = UserDatabase.getDatabase(application);
userDao = db.userDao();
allUsers = userDao.getAll();
}
public LiveData<List<User>> getAllData() { return allUsers; }
public void insert(User user){
UserDatabase.databaseWriteExecutor.execute(() -> {
userDao.insert(user);
});
}
public void deleteAll(){
UserDatabase.databaseWriteExecutor.execute(() -> {
userDao.deleteAll();
});
}
}
UserViewModel
public class UserViewModel extends AndroidViewModel {
public static UserRepository repository;
public final LiveData<List<User>> allUsers;
public UserViewModel(@NonNull Application application) {
super(application);
repository = new UserRepository(application);
allUsers = repository.getAllData();
}
public LiveData<List<User>> getAllUsers() { return allUsers; }
public static void insert(User user) { repository.insert(user); }
}