Home > Mobile >  I can't reach database inspector to add data
I can't reach database inspector to add data

Time:03-08

My app builds successfully, but i cannot reach database inspector. Whenever i open database inspector, it does not show me any database. I tried restarting things, but those didn't work. I tried inspector in another app which i download from google courses, it was working there. I'm stuck here, how can i get over this.

Thats what i see when i open database inspector: enter image description here

Entity

@Entity
data class Note(
    @PrimaryKey(autoGenerate = true)
    val id: Long = 0,
    @ColumnInfo(name = "note_text")
    val noteText: String,
    @ColumnInfo(name = "note_date")
    val noteDate: Date
)

Dao

@Dao
interface NoteDao {

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun insert(note: Note)

    @Update
    suspend fun update(note: Note)

    @Delete
    suspend fun delete(note: Note)

    @Query("SELECT * FROM Note WHERE id = :id")
    fun getNote(id: Long): Flow<Note>

    @Query("SELECT * from Note ORDER BY note_date DESC")
    fun getAllNotes(): Flow<List<Note>>
}

RoomDatabase

private const val TAG = "NoteRoomDatabase"

@Database(entities = [Note::class], version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class NoteRoomDatabase: RoomDatabase() {

    abstract fun noteDao(): NoteDao

    companion object {
        @Volatile
        private var INSTANCE: NoteRoomDatabase? = null

        fun getDatabase(context: Context): NoteRoomDatabase {
            return INSTANCE ?: synchronized(this) {
                val instance = Room.databaseBuilder(
                    context.applicationContext,
                    NoteRoomDatabase::class.java,
                    "note_database")
                    .fallbackToDestructiveMigration()
                    .build()

                Log.d(TAG, "Database created!")
                INSTANCE = instance
                return instance
            }
        }

    }
}

Application

class NotesApplication : Application() {

    val database: NoteRoomDatabase by lazy { NoteRoomDatabase.getDatabase(this) }
}

ViewModelFactory

class AppViewModelFactory(private val noteDao: NoteDao) : ViewModelProvider.Factory {
    override fun <T : ViewModel> create(modelClass: Class<T>): T {
        if (modelClass.isAssignableFrom(AppViewModel::class.java)) {
            @Suppress("UNCHECKED_CAST")
            return AppViewModel(noteDao) as T
        }
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

How i call it from fragments

private val viewModel: AppViewModel by activityViewModels {
    AppViewModelFactory(
        (activity?.application as NotesApplication).database.noteDao()
    )
}

CodePudding user response:

for that you have to create your database first .. when you run your activity this doesn't execute your database classes, so for that in your first main activity

 lifecycleScope.launch(Dispatchers.IO){
                 val database: NoteRoomDatabase by lazy { NoteRoomDatabase.getDatabase(this)
             
               
            }

CodePudding user response:

The reason that you see nothing is because there is nothing until an attempt is made to access the database.

You can force an open of the database by getting a SupportSQLiteDatabase (even if you don't use it)

e.g.

val database: NoteRoomDatabase by lazy { NoteRoomDatabase.getDatabase(this) }
database.openHelper.writableDatabase //<<<<<<<<<< ADDED

You could alternately force an open within the getDatabase function after the build e.g.

instance.openHelper.writableDatabase
  • Related