Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 433
M. Ather Khan Avatar asked Aug 13 '19 07:08

M. Ather Khan


People also ask

What is the difference between SQLite and room database in Android?

Realm is an easy-to-use, open-source alternative to SQLite. The key difference between the two is that Realm is an object database management system, while SQLite is a relational database management system. Realm is often used to replace SQLite. Let's take a closer look at both databases.

What is Dao in room?

Data Access Objects are the main classes where you define your database interactions. They can include a variety of query methods. The class marked with @Dao should either be an interface or an abstract class. At compile time, Room will generate an implementation of this class when it is referenced by a Database .

What are the components of room database in Android?

While developing an app with Room, we need to implement all of three main components: Entity, Dao, Database.

What is foreign key in room database?

Foreign keys allows you to specify constraints across Entities such that SQLite will ensure that the relationship is valid when you modify the database. When a foreign key constraint is specified, SQLite requires the referenced columns to be part of a unique index in the parent table or the primary key of that table.


1 Answers

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
}
like image 109
HandyPawan Avatar answered Sep 27 '22 17:09

HandyPawan