15

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!

M. Ather Khan
  • 305
  • 1
  • 2
  • 9
  • You got a version of your database with one table. Then you update it and Room asked you to increase the version of your database. Is it so? Room is trying to help you *"Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number."*. – Blind Kai Aug 13 '19 at 07:49

5 Answers5

35

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 {
...
}
Bach Vu
  • 2,298
  • 1
  • 15
  • 19
4

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.

Blind Kai
  • 514
  • 5
  • 14
  • In case someone needs to know, clearing up the cache and data is needed and an uninstall wouldn't work. This is because the default behavior after Android 6 is to save the data – Sanved Jun 22 '22 at 03:12
3

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
}
HandyPawan
  • 1,018
  • 1
  • 11
  • 16
1

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();
});
Félix Maroy
  • 1,389
  • 2
  • 16
  • 21
0

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.

Mohit Mahajan
  • 47
  • 1
  • 7