2015-12-13 3 views
3

Я получаю эту ошибку, когда пытаюсь получить читаемую или записываемую базу данных. "SQLiteCantOpenDatabaseException: неизвестная ошибка (код 14) Не удалось открыть базу данных" У меня возникла эта странная проблема с SQLiteOpenHelper, и я видел, что некоторые ответы похожи на эту же проблему, но ни одна из них не решила мою проблему. Так же отчаянно, как и я, я отправляю свой код ниже в надежде, что кто-то найдет что-то, чего я не вижу ...SQLiteCantOpenDatabaseException: неизвестная ошибка (код 14) Не удалось открыть базу данных

(Одна важная информация: я получаю это приложение от другого рабочего. время, которое я отлаживаю, я все еще не изменил имя db. Что было с именем предыдущего приложения (ks.db). Этот db существует на моем устройстве отладки, но tr.db этого не делает)

My DatabaseHelper класс, как это:

//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; 
    } 

мой андроид манифеста:

...

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

И в основной деятельности:

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

Это полный стек ошибок:

> 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

Почему ты жёстко этого ** смешного пути ** 'частного статический окончательная Строка PATH_DB_DEBUG =«/ шоссе/SDCard /»;'? –

+0

Я сделал это, чтобы избежать использования моего устройства для проверки базы данных. Итак, в конструкторе было следующее: 'private DatabaseHelper (контекст контекста) { super (context, (BuildConfig.BUILD_TYPE ==" debug ")? PATH_DB_DEBUG + DB_NAME: DB_NAME, null, VERSION); } 'Это работало для других двух приложений, но я получаю эту ошибку на этом. – Totalys

+0

Вы должны разработать с помощью эмулятора. Только после того, как вы удовлетворены, используйте свое устройство для окончательных испытаний. Во всяком случае, этот путь забавный, потому что не все устройства монтируют SD-карту в этом пути. –

ответ

4

Ошибка перестал показывать после того как я изменен targetSdkVersion от 23 до 22. Это понижение forc чтобы удалить пакет на устройстве, и когда он был переустановлен, он работал нормально. Спасибо Frank N. Stein за помощь!

7

Вы используете SQLiteDatabase.openDatabase в пути к файлу, который может не существовать. Добавьте ниже 2 линии непосредственно перед SQLiteDatabase.openDatabase вызовом

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; 
} 
Смежные вопросы

 Смежные вопросы