问题
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)
回答1:
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;
}
回答2:
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!
来源:https://stackoverflow.com/questions/34253785/sqlitecantopendatabaseexception-unknown-error-code-14-could-not-open-database