3

I'm getting this error when I'm trying to get a readable or writable database. "SQLiteCantOpenDatabaseException: unknown error (code 14) Could not open database" I'm having this weird problem with SQLiteOpenHelper and I've seen some similars answer about this same issue here but none of them solved my problem. As desperate as I am, I'm posting my code below in hope some one find something I'm not seeing...

(One important information: I get this app from another working one. But the very firt time I debug it, I still have not changed the the dbname. Which was with the name of the previous app (ks.db). This db exists at my debug device but tr.db doesn't)

My DatabaseHelper class is like this:

//Singleton
private static DatabaseHelper sInstance;

//Log
private static String TAG = "Database";

private static final String NAME_DB = "tr.db";
private static final String PATH_DB_DEBUG = "/mnt/sdcard/";
private static final int VERSION = 4;

//TABLE NAMES
private static final String TABLE_USERS = "users";
private static final String TABLE_TRACKS = "tracks";
private static final String TABLE_ROTAS = "rotas";
...
/**
     *
     * @param context
     * @return Unique instance of DataBaseHelper
     */
    public static synchronized DatabaseHelper getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return sInstance;
    }

my android manifest:

...

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> ...

And in the main activity:

    //DB
    DatabaseHelper database;
...
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
...
        //database
        database = DatabaseHelper.getInstance(this.getApplicationContext());
        database.getReadableDatabase(); ( <- Error HERE)

This is the full error stack:

> 12-13 15:02:43.798    5764-5764/com.snitram.toriders E/SQLiteLog﹕ (14)
> cannot open file at line 31307 of [2ef4f3a5b1] 12-13 15:02:43.798   
> 5764-5764/com.snitram.toriders E/SQLiteLog﹕ (14) os_unix.c:31307: (2)
> open(/storage/emulated/0tr.db) - 12-13 15:02:43.811   
> 5764-5764/com.snitram.toriders E/SQLiteDatabase﹕ Failed to open
> database '/storage/emulated/0tr.db'.
>     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:207)
>             at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
>             at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
>             at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
>             at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
>             at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
>             at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
>             at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
>             at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
>             at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
>             at com.snitram.toriders.activities.LoginActivity.onCreate(LoginActivity.java:222)
>             at android.app.Activity.performCreate(Activity.java:6245)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
>             at android.app.ActivityThread.-wrap11(ActivityThread.java)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:148)
>             at android.app.ActivityThread.main(ActivityThread.java:5443)
>             at java.lang.reflect.Method.invoke(Native Method)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-13
> 15:02:43.814    5764-5764/com.snitram.toriders E/SQLiteOpenHelper﹕
> Couldn't open /storage/emulated/0tr.db for writing (will try
> read-only):
>     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:207)
>             at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
>             at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
>             at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
>             at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
>             at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
>             at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:571)
>             at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:269)
>             at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
>             at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
>             at com.snitram.toriders.activities.LoginActivity.onCreate(LoginActivity.java:222)
>             at android.app.Activity.performCreate(Activity.java:6245)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
>             at android.app.ActivityThread.-wrap11(ActivityThread.java)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:148)
>             at android.app.ActivityThread.main(ActivityThread.java:5443)
>             at java.lang.reflect.Method.invoke(Native Method)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-13
> 15:02:43.815    5764-5764/com.snitram.toriders E/SQLiteLog﹕ (14)
> cannot open file at line 31307 of [2ef4f3a5b1] 12-13 15:02:43.815   
> 5764-5764/com.snitram.toriders E/SQLiteLog﹕ (14) os_unix.c:31307: (2)
> open(/storage/emulated/0tr.db) - 12-13 15:02:43.816   
> 5764-5764/com.snitram.toriders E/SQLiteDatabase﹕ Failed to open
> database '/storage/emulated/0tr.db'.
>     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:207)
>             at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
>             at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
>             at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
>             at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
>             at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
>             at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:234)
>             at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
>             at com.snitram.toriders.activities.LoginActivity.onCreate(LoginActivity.java:222)
>             at android.app.Activity.performCreate(Activity.java:6245)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
>             at android.app.ActivityThread.-wrap11(ActivityThread.java)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:148)
>             at android.app.ActivityThread.main(ActivityThread.java:5443)
>             at java.lang.reflect.Method.invoke(Native Method)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 12-13
> 15:02:43.816    5764-5764/com.snitram.toriders D/AndroidRuntime﹕
> Shutting down VM 12-13 15:02:43.817    5764-5764/com.snitram.toriders
> E/AndroidRuntime﹕ FATAL EXCEPTION: main
>     Process: com.snitram.toriders, PID: 5764
>     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.snitram.toriders/com.snitram.toriders.activities.LoginActivity}:
> android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error
> (code 14): Could not open database
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2426)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
>             at android.app.ActivityThread.-wrap11(ActivityThread.java)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:148)
>             at android.app.ActivityThread.main(ActivityThread.java:5443)
>             at java.lang.reflect.Method.invoke(Native Method)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
>      Caused by: 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:207)
>             at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:191)
>             at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
>             at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
>             at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
>             at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
>             at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
>             at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:234)
>             at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
>             at com.snitram.toriders.activities.LoginActivity.onCreate(LoginActivity.java:222)
>             at android.app.Activity.performCreate(Activity.java:6245)
>             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
>             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
>             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
>             at android.app.ActivityThread.-wrap11(ActivityThread.java)
>             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
>             at android.os.Handler.dispatchMessage(Handler.java:102)
>             at android.os.Looper.loop(Looper.java:148)
>             at android.app.ActivityThread.main(ActivityThread.java:5443)
>             at java.lang.reflect.Method.invoke(Native Method)
>             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
>             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
Totalys
  • 445
  • 2
  • 10
  • 25
  • 1
    Why did you hardcode this **funny path** `private static final String PATH_DB_DEBUG = "/mnt/sdcard/";`? – Phantômaxx Dec 13 '15 at 17:17
  • I did this to avoid to root my device to inspect the database. So, did the following at the constructor: `private DatabaseHelper(Context context) { super(context, (BuildConfig.BUILD_TYPE == "debug") ? PATH_DB_DEBUG + DB_NAME : DB_NAME, null, VERSION); }` This worked for other two apps but I'm getting this error at this one. – Totalys Dec 13 '15 at 17:29
  • You should develop using an emulator. Only after you are satisfied, use your device for the final tests. Anyway, that path is funny, because not all the devices mount the sdcard in that path. – Phantômaxx Dec 13 '15 at 17:31
  • Unfortunately, my machine is not so good. Emulating takes a lot of extra time... :( . I agree the path is weird but this same path was working for the other two apps :( – Totalys Dec 13 '15 at 17:36
  • 1
    Thank you for openning my eyes. I removed the funny path and now it is working. Is there a secure storage path to use? – Totalys Dec 13 '15 at 17:40
  • `takes a lot of extra time` This motivates developers writing better (optimized) code. You shoud ask Android the path for the sd card. Something like [getExternalStorageDirectory()](http://developer.android.com/intl/es/reference/android/os/Environment.html#getExternalStorageDirectory()). – Phantômaxx Dec 13 '15 at 17:40
  • I've tried this... see it : `//private static final String PATH_DB_DEBUG = "/mnt/sdcard/"; private static final String PATH_DB_DEBUG = Environment.getExternalStorageDirectory().getPath() + "/";`but this didn't solve the problem :( – Totalys Dec 13 '15 at 17:43
  • 1
    You should debug that path and see what does it return. Maybe, you have to fiddle a bit around that. Or try the other Environment methods to get different paths. – Phantômaxx Dec 13 '15 at 18:01

2 Answers2

8

You are using SQLiteDatabase.openDatabase on a file path that may not exist. Add below 2 lines just before SQLiteDatabase.openDatabase call

   private boolean checkDataBase() {
    SQLiteDatabase checkDB = null;
    try {

        String myPath = DB_PATH + DB_NAME;

        File file = new File(myPath);
        if (file.exists() && !file.isDirectory())
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
    } catch (SQLiteException e) {
        // database does't exist yet.
    }

    if (checkDB != null) {
        checkDB.close();
    }

    return checkDB != null ? true : false;
}
Amit
  • 409
  • 1
  • 5
  • 12
6

Error stopped to show after I changed the targetSdkVersion from 23 to 22. This downgrade forced to uninstall the package at the device and when It was reinstalled it worked fine. Thank you Frank N. Stein for the help!

Community
  • 1
  • 1
Totalys
  • 445
  • 2
  • 10
  • 25
  • This is an old post i know, however this solution worked for me. Can you please explain what exactly happened that helped in resolving the issue? – abhogu Nov 14 '18 at 06:55
  • I'm sorry I did not investigate it any further after started working. I guess it was a dll conflict at that time. Seems that removing the latest assembly solved the issue. – Totalys Jun 07 '19 at 19:14