当我试图获得可读或可写的数据库时,我收到此错误。 “SQLiteCantOpenDatabaseException:未知错误(代码14)无法打开数据库” 我遇到了SQLiteOpenHelper这个奇怪的问题,我看到一些类似的问题在这里回答了同样的问题,但没有一个能解决我的问题。 像我一样绝望,我在下面发布我的代码,希望有人发现我没看到的东西......
(一个重要的信息:我从另一个工作中得到这个应用程序。但是我调试它的非常时间,我仍然没有更改dbname。这与前一个应用程序的名称(ks.db)有关。此数据库存在于我的调试设备中,但tr.db不存在)
我的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)
答案 0 :(得分:8)
您在可能不存在的文件路径上使用SQLiteDatabase.openDatabase
。在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;
}
答案 1 :(得分:5)
将 targetSdkVersion 从23更改为22后,错误停止显示。 这次降级被迫卸载设备上的软件包,当它重新安装时,它工作正常。感谢您Frank N. Stein的帮助!