我正在尝试学习sqlite for android。 我创建了一个Helper类,它应该打开我的数据库并执行帮助类所做的任何事情。
我的问题是我甚至无法打开这个错误的数据库:
android.database.sqlite.SQLiteCantOpenDatabaseException:未知错误(代码14):无法打开数据库
这是我的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;
}
这一切都被try catch所包围,但问题仍然在于它无法打开我的数据库。这是Stack Trace。
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,因为我想要更多地控制操作,因为我试图只为我的所有类使用一个帮助器。
答案 0 :(得分:2)
调用SQLiteDatabase.openOrCreateDatabase
时,第一个参数(String path
)应该是文件系统的完整路径,请从getFilesDir()
或类似的调用
答案 1 :(得分:0)
你应该在DatabaseHelper构造函数上调用super。
//constructor
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
ctx = context;
DATABASE_PATH = ctx.getFilesDir().getPath();
}