Android机房无法打开数据库

时间:2018-06-22 17:55:31

标签: android android-sqlite android-room

从房间dao查询时崩溃。有人有想法吗? 它在nexus 5 android 6.0上运行良好,但在nokia 8 android 8.1上崩溃

06-23 01:19:02.469 2652-2729/com.aaa.bbb E/SQLiteLog: (14) cannot open file at line 35807 of [605907e73a]
    (14) os_unix.c:35807: (21) open(/data/user/0/com.aaa.bbb/files) - 
06-23 01:19:02.472 2652-2729/com.aaa.bbb E/SQLiteDatabase: Failed to open database '/data/user/0/com.aaa.bbb/files'.
    android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
        at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
        at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
        at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:493)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:200)
        at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:192)
        at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:864)
        at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:849)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:724)
        at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:714)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:295)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:238)
        at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase(FrameworkSQLiteOpenHelper.java:96)
        at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase(FrameworkSQLiteOpenHelper.java:54)
        at android.arch.persistence.room.RoomDatabase.query(RoomDatabase.java:233)
        at com.aaa.bbb.manager.db.CityAreaDao_Impl$4.compute(CityAreaDao_Impl.java:229)
        at com.aaa.bbb.manager.db.CityAreaDao_Impl$4.compute(CityAreaDao_Impl.java:215)
        at android.arch.lifecycle.ComputableLiveData$2.run(ComputableLiveData.java:100)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)

-------------------数据库管理器-------------------

@Database(entities = [(EMCityArea::class)], exportSchema = false, version = 1)
private abstract class EMDataBase : RoomDatabase() {
    abstract fun cityAreakDao():CityAreaDao
}

class EMDBManager private constructor(){
    companion object {
        val share: EMDBManager by lazy { EMDBManager() }
    }

    private val db = Room.databaseBuilder(App.applicationContext(), EMDataBase::class.java,"dbfile.db").build()

    val cityAreaDao:CityAreaDao
        get() = db.cityAreakDao()

    fun allCountry():LiveData<List<EMCityArea>>{
        return cityAreaDao.allCounty()
    }
}

-------------------查询-------------------

EMDBManager.share.allCountry().observe(this, Observer {coutryItems ->
    if (coutryItems == null){return@Observer}
    mCountryAdapter.clear()
    mCountryAdapter.addAll(coutryItems.mapNotNull { it.name })
    mCountryAdapter.notifyDataSetChanged()
})

1 个答案:

答案 0 :(得分:0)

崩溃是由我的应用程序中的覆盖getDatabasePath引起的

override fun getDatabasePath(name: String?): File {
    return filesDir
}

就我而言,只需删除该方法即可。