I'm starting in the world of kotlin and when I say starting is that I'm watching all kinds of tutorials, from My First App in Android Developer, through YouTube and other tutorials, on a particular page link explain how to read an existing BD and I was thrilled to replicate it, it's a basic code of reading a BD in the assets / databases folder and when executing it in the androidStudio emulator, it gives me this message:
Here is the structure of the project that shows the location of the database:
Here is the code I used:
MainActivity.kt
package com.example.example
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// This is an example, remember to use a background thread in production.
val myDatabase = MyDatabaseHelper(this).readableDatabase
myDatabase.rawQuery("SELECT * FROM Region", null)
}
}
MyDatabaseHelper.kt
package com.example.example
import android.content.Context
import android.content.SharedPreferences
import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper
import java.io.File
import java.io.FileOutputStream
class MyDatabaseHelper(val context: Context) : SQLiteOpenHelper(context, DATABASE_NAME, null, DATABASE_VERSION) {
private val preferences: SharedPreferences = context.getSharedPreferences(
"${context.packageName}.database_versions",
Context.MODE_PRIVATE
)
private fun installedDatabaseIsOutdated(): Boolean {
return preferences.getInt(DATABASE_NAME, 0) < DATABASE_VERSION
}
private fun writeDatabaseVersionInPreferences() {
preferences.edit().apply {
putInt(DATABASE_NAME, DATABASE_VERSION)
apply()
}
}
private fun installDatabaseFromAssets() {
val inputStream = context.assets.open("$ASSETS_PATH/$DATABASE_NAME.sqlite3")
try {
val outputFile = File(context.getDatabasePath(DATABASE_NAME).path)
val outputStream = FileOutputStream(outputFile)
inputStream.copyTo(outputStream)
inputStream.close()
outputStream.flush()
outputStream.close()
} catch (exception: Throwable) {
throw RuntimeException("The $DATABASE_NAME database couldn't be installed.", exception)
}
}
@Synchronized
private fun installOrUpdateIfNecessary() {
if (installedDatabaseIsOutdated()) {
context.deleteDatabase(DATABASE_NAME)
installDatabaseFromAssets()
writeDatabaseVersionInPreferences()
}
}
override fun getWritableDatabase(): SQLiteDatabase {
throw RuntimeException("The $DATABASE_NAME database is not writable.")
}
override fun getReadableDatabase(): SQLiteDatabase {
installOrUpdateIfNecessary()
return super.getReadableDatabase()
}
override fun onCreate(db: SQLiteDatabase?) {
// Nothing to do
}
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {
// Nothing to do
}
companion object {
const val ASSETS_PATH = "databases"
const val DATABASE_NAME = "mydb"
const val DATABASE_VERSION = 1
}
}
Here is the exit of Logcat :
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.example, PID: 5700
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example/com.example.example.MainActivity}: java.lang.RuntimeException: The mydb database couldn't be installed.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.RuntimeException: The mydb database couldn't be installed.
at com.example.example.MyDatabaseHelper.installDatabaseFromAssets(MyDatabaseHelper.kt:41)
at com.example.example.MyDatabaseHelper.installOrUpdateIfNecessary(MyDatabaseHelper.kt:49)
at com.example.example.MyDatabaseHelper.getReadableDatabase(MyDatabaseHelper.kt:59)
at com.example.example.MainActivity.onCreate(MainActivity.kt:12)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.io.FileNotFoundException: /data/data/com.example.example/databases/mydb: open failed: ENOENT (No such file or directory)
at libcore.io.IoBridge.open(IoBridge.java:456)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at com.example.example.MyDatabaseHelper.installDatabaseFromAssets(MyDatabaseHelper.kt:33)
at com.example.example.MyDatabaseHelper.installOrUpdateIfNecessary(MyDatabaseHelper.kt:49)
at com.example.example.MyDatabaseHelper.getReadableDatabase(MyDatabaseHelper.kt:59)
at com.example.example.MainActivity.onCreate(MainActivity.kt:12)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:442)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at java.io.FileOutputStream.<init>(FileOutputStream.java:72)
at com.example.example.MyDatabaseHelper.installDatabaseFromAssets(MyDatabaseHelper.kt:33)
at com.example.example.MyDatabaseHelper.installOrUpdateIfNecessary(MyDatabaseHelper.kt:49)
at com.example.example.MyDatabaseHelper.getReadableDatabase(MyDatabaseHelper.kt:59)
at com.example.example.MainActivity.onCreate(MainActivity.kt:12)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Application terminated.
Any ideas that can help me?