Android 数据库文件已复制但无法打开
Android db file is copied but failed to open
我正在开发 android 应用程序。
首次安装时,它会从 Firebase (firestore) 下载数据并存储到本地数据库中。
但是性能太差了。
所以我决定创建一个 db 文件,并将其包含到 APK 中。
那么应用程序就不需要从Firebase下载数据了。
private fun provideDatabase(context: Context): MhwDatabase {
return instance ?: generateDatabase(context)
}
fun generateDatabase(context: Context): MhwDatabase {
copyAttachedDatabase(context, "mhw.db")
return Room.databaseBuilder(context.applicationContext, MhwDatabase::class.java, "mhw.db")
.addMigrations(MIGRATION_1_2)
.build().apply {
instance = this
}
}
fun copyAttachedDatabase(context: Context, databaseName: String) {
Log.d(TAG, "[MHW] copyAttachedDatabase start")
val dbPath = context.getDatabasePath(databaseName)
// If the database already exists, return
if (dbPath.exists()) {
Log.d(TAG, "[MHW] copyAttachedDatabase already exist")
return
}
// Make sure we have a path to the file
dbPath.parentFile.mkdirs()
// Try to copy database file
try {
val br = BufferedReader(InputStreamReader(context.assets.open("databases/$databaseName")))
val bw = BufferedWriter(FileWriter(dbPath))
Log.d(TAG, "[MHW] write db file: $dbPath")
var line: String? = null;
while ({ line = br.readLine(); line }() != null) {
bw.write(line)
}
bw.flush()
bw.close()
br.close()
} catch (e: IOException) {
Log.e(TAG, "[MHW] Failed to open file", e)
e.printStackTrace()
}
Log.d(TAG, "[MHW] copyAttachedDatabase done")
}
这段代码工作正常。
没有error/exceptions.
但是当我尝试从本地数据库读取数据时,失败并显示以下日志。
09-16 23:00:52.467 31922-32011/com.eastriver.mhwdb E/SQLiteLog: (11) database corruption at line 54610 of [2ef4f3a5b1]
09-16 23:00:52.468 31922-32011/com.eastriver.mhwdb E/SQLiteLog: (11) database corruption at line 54651 of [2ef4f3a5b1]
(11) statement aborts at 9: []
(11) database disk image is malformed
09-16 23:00:52.484 31922-32011/com.eastriver.mhwdb E/SQLiteLog: (11) database corruption at line 54610 of [2ef4f3a5b1]
(11) database corruption at line 54651 of [2ef4f3a5b1]
(11) statement aborts at 9: []
(11) database disk image is malformed
09-16 23:00:52.487 31922-32011/com.eastriver.mhwdb E/SQLiteDatabase: Failed to open database '/data/user/0/com.eastriver.mhwdb/databases/mhw.db'.
android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11): , while compiling: PRAGMA journal_mode
为了获取数据库文件,我在 Android Studio 中使用了 "Device File Explorer"。
首先,应用程序从 firebase 下载数据,并存储到本地数据库中。
之后,我复制了db文件,并使用它。
我使用 "Room" 作为本地数据库。
另外,当我得到 db 文件时,我使用 "DB browser for SQLite" 桌面应用程序打开它。
我确认数据库有效。
但是在数据库复制完成后,我在 Android Studio 中使用 "Device File Explorer" 获取数据库文件,并尝试使用 "DB Browser for SQLite" 读取数据库文件。
但失败并显示错误消息:"database disk image is malformed".
我认为...是 "Room" 的原因??
存在此问题的原因可能是您尝试将二进制格式的数据库文件读取为文本。如果您不处理文本,请不要使用 Reader
/Writer
接口,因为隐含的文本编码会导致它弄乱数据。直接用BufferedInputStream
/BufferedOutputStream
直接把字节流复制到新文件就可以了
我正在开发 android 应用程序。 首次安装时,它会从 Firebase (firestore) 下载数据并存储到本地数据库中。 但是性能太差了。 所以我决定创建一个 db 文件,并将其包含到 APK 中。 那么应用程序就不需要从Firebase下载数据了。
private fun provideDatabase(context: Context): MhwDatabase {
return instance ?: generateDatabase(context)
}
fun generateDatabase(context: Context): MhwDatabase {
copyAttachedDatabase(context, "mhw.db")
return Room.databaseBuilder(context.applicationContext, MhwDatabase::class.java, "mhw.db")
.addMigrations(MIGRATION_1_2)
.build().apply {
instance = this
}
}
fun copyAttachedDatabase(context: Context, databaseName: String) {
Log.d(TAG, "[MHW] copyAttachedDatabase start")
val dbPath = context.getDatabasePath(databaseName)
// If the database already exists, return
if (dbPath.exists()) {
Log.d(TAG, "[MHW] copyAttachedDatabase already exist")
return
}
// Make sure we have a path to the file
dbPath.parentFile.mkdirs()
// Try to copy database file
try {
val br = BufferedReader(InputStreamReader(context.assets.open("databases/$databaseName")))
val bw = BufferedWriter(FileWriter(dbPath))
Log.d(TAG, "[MHW] write db file: $dbPath")
var line: String? = null;
while ({ line = br.readLine(); line }() != null) {
bw.write(line)
}
bw.flush()
bw.close()
br.close()
} catch (e: IOException) {
Log.e(TAG, "[MHW] Failed to open file", e)
e.printStackTrace()
}
Log.d(TAG, "[MHW] copyAttachedDatabase done")
}
这段代码工作正常。 没有error/exceptions.
但是当我尝试从本地数据库读取数据时,失败并显示以下日志。
09-16 23:00:52.467 31922-32011/com.eastriver.mhwdb E/SQLiteLog: (11) database corruption at line 54610 of [2ef4f3a5b1]
09-16 23:00:52.468 31922-32011/com.eastriver.mhwdb E/SQLiteLog: (11) database corruption at line 54651 of [2ef4f3a5b1]
(11) statement aborts at 9: []
(11) database disk image is malformed
09-16 23:00:52.484 31922-32011/com.eastriver.mhwdb E/SQLiteLog: (11) database corruption at line 54610 of [2ef4f3a5b1]
(11) database corruption at line 54651 of [2ef4f3a5b1]
(11) statement aborts at 9: []
(11) database disk image is malformed
09-16 23:00:52.487 31922-32011/com.eastriver.mhwdb E/SQLiteDatabase: Failed to open database '/data/user/0/com.eastriver.mhwdb/databases/mhw.db'.
android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed (code 11): , while compiling: PRAGMA journal_mode
为了获取数据库文件,我在 Android Studio 中使用了 "Device File Explorer"。 首先,应用程序从 firebase 下载数据,并存储到本地数据库中。 之后,我复制了db文件,并使用它。
我使用 "Room" 作为本地数据库。
另外,当我得到 db 文件时,我使用 "DB browser for SQLite" 桌面应用程序打开它。 我确认数据库有效。
但是在数据库复制完成后,我在 Android Studio 中使用 "Device File Explorer" 获取数据库文件,并尝试使用 "DB Browser for SQLite" 读取数据库文件。 但失败并显示错误消息:"database disk image is malformed".
我认为...是 "Room" 的原因??
存在此问题的原因可能是您尝试将二进制格式的数据库文件读取为文本。如果您不处理文本,请不要使用 Reader
/Writer
接口,因为隐含的文本编码会导致它弄乱数据。直接用BufferedInputStream
/BufferedOutputStream
直接把字节流复制到新文件就可以了