Vikash Kumar
Vikash Kumar

Reputation: 395

Getting Issue in reading sqllite database

I have created a database using Singleton Approach in external drive and when i have created an object for the DatabaseHelper to access some method(eg checkMISSetting) i got an issue.

I have also attached my code snippet below:

Handler class where i have create database:

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;
    }

Calling Handler method

    DatabaseHandler handler=DatabaseHandler.getInstance(this);

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

    handler.close();

Method where i'm getting an error

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;
        }

    }

In this method i'm getting an issue while performing getReadableDatabase() that i'm attaching here:

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)

Upvotes: 4

Views: 90

Answers (1)

Pramod Waghmare
Pramod Waghmare

Reputation: 1283

Try this

Replace your constructor of DatabaseHandler class

private DatabaseHandler(Context context) {

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

}

Upvotes: 2

Related Questions