android – 在读取sqllite数据库时出现问题

我在外部驱动器中使用Singleton Approach创建了一个数据库,当我为
DatabaseHelper创建了一个访问某些方法的对象(例如checkMISSetting)时,我遇到了一个问题.

我还在下面附上了我的代码段:

Handler类我在哪里创建数据库:

private DatabaseHandler(Context context) {

    super(context, context.getExternalFilesDir(null).getAbsolutePath()
            + "/" + "Android/MobInvSuit" + "/" + DATABASE_NAME, null,
            DATABASE_VERSION);

}

public static synchronized DatabaseHandler getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new DatabaseHandler(context.getApplicationContext());
        }
        return sInstance;
    }

调用Handler方法

    DatabaseHandler handler=DatabaseHandler.getInstance(this);

    boolean check = handler.checkMISSetting();//Line number 129

    handler.close();

我收到错误的方法

public boolean checkMISSetting() {
        boolean flag = false;
        SQLiteDatabase db = getReadableDatabase();
        String q = "SELECT COUNT(*) FROM " + DatabaseHandler.TABLE_SETTINGS;

        int numRows = (int) DatabaseUtils.longForQuery(db, q, null);

        if (numRows > 0) {
            flag = true;
            return flag;
        } else {
            return flag;
        }

    }

在这个方法中,我在执行getIadableDatabase()时遇到问题,我在这里附加:

06-21 09:44:03.932: E/SQLiteDatabase(1685): Failed to open database '/storage/emulated/0/Android/data/com.example.mobinventorysuit/files/Android/MobInvSuit/mis.db'.
06-21 09:44:03.932: E/SQLiteDatabase(1685): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:209)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:804)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:789)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:235)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at com.mis.database.DatabaseHandler.checkMISSetting(DatabaseHandler.java:2912)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at com.mis.common.MainActivity.onCreate(MainActivity.java:129)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.Activity.performCreate(Activity.java:5231)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.os.Handler.dispatchMessage(Handler.java:102)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.os.Looper.loop(Looper.java:136)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at android.app.ActivityThread.main(ActivityThread.java:5001)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at java.lang.reflect.Method.invokeNative(Native Method)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at java.lang.reflect.Method.invoke(Method.java:515)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
06-21 09:44:03.932: E/SQLiteDatabase(1685):     at dalvik.system.NativeStart.main(Native Method)

最佳答案 试试这个

替换DatabaseHandler类的构造函数

private DatabaseHandler(Context context) {

    super(context,Environment.getExternalStorageDirectory()+ "/" + "Android/MobInvSuit" + "/" + DATABASE_NAME, null,DATABASE_VERSION);

}
点赞