7

I am using Room API to implement a DB in my Android app. It seems that every time I load my app it tries to create the database again and again. Is there any way to restrict this?

 db = Room.databaseBuilder(context, AppDatabase.class, "database-name").build();
5
  • what type of variable is db ? Commented Mar 13, 2018 at 11:56
  • @GastónSaillén its an object of RoomDatabase Commented Mar 13, 2018 at 11:59
  • 2
    Possible duplicate of Query if Android database exists! Commented Mar 13, 2018 at 11:59
  • @HemantParmar . This is not a duplicate. Here I am using the android Room API not the SQLlite one Commented Mar 13, 2018 at 12:09
  • Have you found a solution to this problem? How do you call an existing database? Commented Jun 5, 2018 at 8:00

4 Answers 4

4

You are using db that is, in fact, a file. You can check, if it exists, this method could be helpful:

private static boolean doesDatabaseExist(Context context, String dbName) {
    File dbFile = context.getDatabasePath(dbName);
    return dbFile.exists();
}
Sign up to request clarification or add additional context in comments.

Comments

3

When you create database, the database onCreate() callback get called when the application starts. you can use the below code in app activity and that activity call in manifest file in application class call like used below code .

public class AppActivity extends Application {

    AppDatabase db;

    @Override
    public void onCreate() {
        super.onCreate();
        db = Room.databaseBuilder(getApplicationContext(), AppDatabase.class, "database-name").build();
    }

    public AppDatabase getDatabase() {
        return db;
    }
}

and add below line manifest file .. add below line in application tag

android:name="AppActivity"

Comments

1

You can get count of entities in db (TODO app - example). entities > 0.

class App: Application() {
    override fun onCreate() {
        super.onCreate()
        instance = this
        database = Room.databaseBuilder(applicationContext,
            AppDatabase::class.java, "database").build()
    }
    companion object {
        lateinit var instance: App
        lateinit var database: AppDatabase
    }
}

//DB class

 @Database(entities = [Task::class], version = 1, exportSchema = false)
       abstract class AppDatabase : RoomDatabase() {
       abstract fun taskDao(): TaskDao
}

//Dao interface

   @Dao
    interface TaskDao {
        @Query("SELECT COUNT(id) FROM tasks")
        fun getTasksCount(): Int
    }

//Model

@Entity(indices = [Index(value = ["title"], unique = true)], tableName ="tasks")
    class Task(
        var title: String = "",
        var description: String = "",
        var date: Date,
        @Embedded 
        var remind: Constants.RemindPeriod = Constants.RemindPeriod.MIN5,
        @Embedded
        var prior: Priority) : Serializable {
        @PrimaryKey(autoGenerate = true)
        var id: Long = 0}

//CheckDB

 private fun checkDatabaseState() {
        doAsync {
            val db = App.database
            val entityCount = db.taskDao().getTasksCount().or(0)
            isDatabaseNotEmpty = entityCount > 0
        }
    }  

1 Comment

and do not forget: implementation "android.arch.persistence.room:runtime:1.1.1" kapt "android.arch.persistence.room:compiler:1.1.1" or you will have RoomDatabase_Impl does not exist
0

Try this

if (!db.exists()) {
    // Database does not exist so copy it from assets here
    Log.i("Database", "Not Found");
} else {
    Log.i("Database", "Found");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.