2015-11-05 142 views
2

我正在嘗試學習android的sqlite。 我創建了一個Helper類,它應該打開我的數據庫並執行任何幫助類。SQLite openOrCreateDatabase未知錯誤(代碼14):無法打開數據庫

我的問題是,我甚至無法打開此錯誤的數據庫淡然:

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

這是我的DatabaseHelper類的相關代碼,錯誤是在openOrCreateDatabase行:

public class DatabaseHelper<T> implements IDatabaseHelper{ 
private static final String DATABASE_NAME = "myDatabase.db"; 
private static String DATABASE_PATH = ""; 
private static final int DATABASE_VERSION = 1; 
private static SQLiteDatabase database; 
private static Context ctx; 

//constructor 
public DatabaseHelper(Context context){ 
    ctx = context; 
    DATABASE_PATH = ctx.getFilesDir().getPath(); 
} 

//apertura database 
public boolean OpenDB() { 
    //try{ 
    database = SQLiteDatabase.openOrCreateDatabase(DATABASE_NAME, null, null); 
    return true; 
    } 

這是所有試圖捕獲sorrounded,但問題仍然是,它無法打開我的數據庫。這裏是堆棧跟蹤。

11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: Failed to open database 'myDatabase.db'. 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at 
android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:717) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at personal.pier.weightwatcher.Utils.DatabaseHelper.OpenDB(DatabaseHelper.java:85) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at personal.pier.weightwatcher.Activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:22) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.Activity.performCreate(Activity.java:5990) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.os.Looper.loop(Looper.java:135) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at android.app.ActivityThread.main(ActivityThread.java:5292) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at java.lang.reflect.Method.invoke(Native Method) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
11-05 17:27:35.410 16641-16641/? E/SQLiteDatabase:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
11-05 17:27:35.410 16641-16641/? D/AndroidRuntime: Shutting down VM 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: FATAL EXCEPTION: main 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: Process: personal.pier.weightwatcher, PID: 16641 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{personal.pier.weightwatcher/personal.pier.weightwatcher.Activities.SplashScreenActivity}: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.access$900(ActivityThread.java:154) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5292) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime: Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:717) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at personal.pier.weightwatcher.Utils.DatabaseHelper.OpenDB(DatabaseHelper.java:85) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at personal.pier.weightwatcher.Activities.SplashScreenActivity.onCreate(SplashScreenActivity.java:22) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.Activity.performCreate(Activity.java:5990) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311) 
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2420)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.access$900(ActivityThread.java:154)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1321)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.os.Handler.dispatchMessage(Handler.java:102)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.os.Looper.loop(Looper.java:135)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at android.app.ActivityThread.main(ActivityThread.java:5292)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Native Method)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at java.lang.reflect.Method.invoke(Method.java:372)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)  
11-05 17:27:35.411 16641-16641/? E/AndroidRuntime:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)  

我真的找不到解決方案。任何幫助將不勝感激。謝謝你的建議

PS:我沒有在porpouse上擴展SQLiteOpenHelper,因爲我想對操作進行更多的控制,因爲我試圖對所有的類只使用一個幫助器。

+1

給它一個完整的路徑在文件系統中的 – pskink

+0

@pskink感謝回覆,我應該怎麼做? getfilesdir()。getpath()是不夠的? –

+0

你用'DATABASE_NAME'調用'openOrCreateDatabase',它只是「myDatabase.db」,而不是像「/full/path/to/my/database.db」這樣的東西 – pskink

回答

1

調用SQLiteDatabase.openOrCreateDatabase當第一個參數(String path)應該是文件系統的完整路徑,從getFilesDir()或類似的呼籲使

0

你應該在DatabaseHelper的構造函數上調用super。

//constructor 
public DatabaseHelper(Context context){ 
    super(context, DATABASE_NAME, null, DATABASE_VERSION); 
    ctx = context; 
    DATABASE_PATH = ctx.getFilesDir().getPath(); 
} 
+0

感謝您快速回復,我收到錯誤,我應該添加一些東西給我的班級使用超級?我得到「Object()在對象中不能應用」 –

+0

我想你應該擴展SQLiteOpenHelper,看到這個例子:[link](http://alvinalexander.com/java/jwarehouse/android/packages/SettingsProvider/src/com /android/providers/settings/DatabaseHelper.java.shtml) – Alisson

+0

nope,我在最後寫到「PS」中,我不想擴展SQLiteOpenHelper,因爲我想爲我的所有實體使用一個幫助類,所以我需要更多的控制。來自@pskink的評論解決了這個問題,當我編寫代碼時,我只是在睡覺。無論如何感謝您的答案! :) –

相關問題