Unable to open database

An error occurred while trying to use "net.zetetic:sqlcipher-android to encrypt sqlite:
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
at net.zetetic.database.sqlcipher.SQLiteConnection.nativeOpen(Native Method)
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:226)
at net.zetetic.database.sqlcipher.SQLiteConnection.open(SQLiteConnection.java:202)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:475)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:189)
at net.zetetic.database.sqlcipher.SQLiteConnectionPool.open(SQLiteConnectionPool.java:181)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openInner(SQLiteDatabase.java:1028)
at net.zetetic.database.sqlcipher.SQLiteDatabase.open(SQLiteDatabase.java:1013)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openDatabase(SQLiteDatabase.java:840)
at net.zetetic.database.sqlcipher.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:919)
at com.example.minespringbootdemo.application.MineApplication.initSqlite(MineApplication.kt:88)
at com.example.minespringbootdemo.application.MineApplication.onCreate(MineApplication.kt:29)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1211)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:7094)
at android.app.ActivityThread.access$1700(ActivityThread.java:275)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2168)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:210)
at android.os.Looper.loop(Looper.java:299)
at android.app.ActivityThread.main(ActivityThread.java:8280)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:576)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1073)

My code is written like this:
val databaseFile = getDatabasePath(“test.db”)
if (!databaseFile.exists()) {
databaseFile.mkdirs()
}
Log.d(“DatabasePath”, “Database Path: ${databaseFile.absolutePath}”)

    val key = ByteArray(64)
    SecureRandom().nextBytes(key)
    System.loadLibrary("sqlcipher")

    mSQLiteDatabase =
        SQLiteDatabase.openOrCreateDatabase(databaseFile, key, null, null, null)
    val createTable = """
    CREATE TABLE mine_test_table (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        name TEXT,
        age INTEGER
    )
""".trimIndent()
    mSQLiteDatabase.execSQL(createTable)

What could be the reason for such a problem?

Hi @CodeLoverlyn,

It’s had to say exactly what is causing the issue for you, however, with a result code of 14, it could be related to the local file system permissions. You don’t mention what device emulator you are experiencing this issue on, what have you tried? Does it happen on multiple devices/emulators?