M. Ather Khan
M. Ather Khan

Reputation: 335

Android Room: One database with multiple tables

I have two separate tables for my application, one is called users and the other is called passwords.

Users:

@Entity(tableName = "users")
public class Users {
   // some setters and getters here
}

Passwords:

@Entity(tableName = "passwords")
public class Passwords {
   // some setters and getters here
}

And this is how I'm accessing the database:

usersdb = Room.databaseBuilder(this, Users.class,"mymaindb")
          .allowMainThreadQueries()
          .build();

// Then later in some other activity when I need to use passwords table

passwords = Room.databaseBuilder(this, passwords.class,"mymaindb")
          .allowMainThreadQueries()
          .build();

The issue I'm having is that after a fresh install, when I access passwords then I can't access users or vice versa.

I get the following error:

Caused by: java.lang.IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.

But when I try to have two separate databases like passwords_db and users_db instead of mymaindb, then it works completely fine.

So is there a way I can have multiple tables under one database? If so, what am I doing wrong then? Thanks in advance!

Upvotes: 17

Views: 38290

Answers (5)

HandyPawan
HandyPawan

Reputation: 1116

It's simple like adding one entity table. Just add another table name using ",". And another table will be added.

@Database(entities = [TableUser::class, TableRecord::class], version = 1)

abstract class MyDatabase : RoomDatabase() {
    abstract fun myDao(): MyDao
}

Upvotes: 4

Félix Maroy
Félix Maroy

Reputation: 1487

Here's another way you can have what you need by using one RoomDB class for all your tables:

@Database(entities = {Users.class, Passwords.class}, version = 1, exportSchema = false)
public abstract class MyDatabase extends RoomDatabase {
private static MyDatabase INSTANCE;

    public abstract UsersDAO usersDAO();
    public abstract PasswordsDAO passwordsDAO();

    public static MyDatabase getDatabase(Context context) {
        if (INSTANCE == null) {
            INSTANCE = Room.databaseBuilder(context.getApplicationContext(),
                    MyDatabase.class, "mydatabase")
                    .build();
        }
        return INSTANCE;
    }
}

Your Entity will look like this:

@Entity(tableName = "user_table")
public class User {

    @PrimaryKey(autoGenerate = true)
    private long id;
    private String username;
    private String email;

    .............//Constructor goes here

    .............//Getters and Setters go here

}

The DAO looks like this:

@Dao
public interface UsersDAO {

    @Query("SELECT * FROM user_table")
    List<User> getAll();

}

Then you'll fetch data like this:

AsyncTask.execute(() -> {               
    MyDatabase.getDatabase(getApplicationContext()).usersDAO().getAll();
});

Upvotes: 1

Mohit Mahajan
Mohit Mahajan

Reputation: 45

if you dont want to change the code you can simply uninstall and install the app again. But this will delete your data saved in database till now.

Upvotes: -1

Bach Vu
Bach Vu

Reputation: 2348

I think you got this all wrong, Room.databaseBuilder should only be called once to setup the database and in that database class, you will construct multiple tables. For example:

Room.databaseBuilder(this, MyRoomDb.class, "mymaindb")
                .allowMainThreadQueries()
                .build()

And your MyRoomDb should look like this

@Database(
        entities = {
            Users.class,
            Passwords.class
        },
        version = VERSION
)
public abstract class MyRoomDb extends RoomDatabase {
...
}

Upvotes: 38

Blind Kai
Blind Kai

Reputation: 524

You have few variants how to solve this problem:

  • Add tables back but increase the version of database;

    @Database(entities={Users.class, Passwords.class}, version = 2)

  • Clean the application settings and build the new database;

Just clean the application cache and try to recreate the database.

Upvotes: 4

Related Questions