Android

SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database

Caused by android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
android.database.sqlite.SQLiteConnection.nativeOpen (SQLiteConnection.java)
android.database.sqlite.SQLiteConnection.open (SQLiteConnection.java:209)
android.database.sqlite.SQLiteConnection.open (SQLiteConnection.java:193)
android.database.sqlite.SQLiteConnectionPool.openConnectionLocked (SQLiteConnectionPool.java:463)
android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:185)
android.database.sqlite.SQLiteConnectionPool.open (SQLiteConnectionPool.java:177)
android.database.sqlite.SQLiteDatabase.openInner (SQLiteDatabase.java:804)
android.database.sqlite.SQLiteDatabase.open (SQLiteDatabase.java:789)
android.database.sqlite.SQLiteDatabase.openDatabase (SQLiteDatabase.java:694)
android.app.ContextImpl.openOrCreateDatabase (ContextImpl.java:990)
android.app.ContextImpl.openOrCreateDatabase (ContextImpl.java:979)
android.content.ContextWrapper.openOrCreateDatabase (ContextWrapper.java:250)

■ SQLiteCantOpenDatabaseException의 원인 
database 폴더가 생성되지않아서 발생

 

■ 발생기기 
API 수준 19  : 306SH

 

■ 해결법 
DB Path를 지정할 때 하드코딩 하지말고 getFileDir()를 사용하라

 

■ 샘플 코드

This may be a little late, but hope this helps for whoever gets this problem (since I can’t find a definitive solution around).

I think I know the reason for this cause (at least for my case). Looking in the DDMS –> File Explorer, you’d realize that the Database Folder (/data/data//databases/) does not exist, which is why the application cannot create the database file in that non-existent folder. If you can create a databases folder in some manner, you can avoid this problem.

Because I’m lazy, I just used the /data/data//files/ folder when I’m in Emulator mode. You can get the files dir using this:

context.getFilesDir().getPath()

This worked beautifully for me in the Emulator.

Hope this helps someone.

In case you want to see some code:

String dbFilename = "example.db"; try {            File databaseFile = getDatabasePath(dbFilename);                 SQLiteDatabase _db = SQLiteDatabase.openOrCreateDatabase(databaseFile); } catch (Exception e) {     String databasePath =  getFilesDir().getPath() +  "/" + dbFilename;     File databaseFile = new File(databasePath);      _db = SQLiteDatabase.openOrCreateDatabase(databaseFile); }

EDIT: I tried logging into Facebook (my app has FB integration) on the Emulator and /databases folder appeared after that (and persisted). Not sure what happened, but it’s possible to create that folder somehow. Something for another expert around here to shed light on.

 

[reference]

  • http://stackoverflow.com/questions/7316191/sqlite-returned-an-error-code-of-14
  • http://stackoverflow.com/questions/33549889/sqlite-openorcreatedatabase-unknown-error-code-14-could-not-open-database

 

Leave a Reply

error: Content is protected !!