我正在嘗試學習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,因爲我想對操作進行更多的控制,因爲我試圖對所有的類只使用一個幫助器。
給它一個完整的路徑在文件系統中的 – pskink
@pskink感謝回覆,我應該怎麼做? getfilesdir()。getpath()是不夠的? –
你用'DATABASE_NAME'調用'openOrCreateDatabase',它只是「myDatabase.db」,而不是像「/full/path/to/my/database.db」這樣的東西 – pskink