Friday, 25 April 2014

In Android, check if sqlite database exists fails from time to time

private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {

        File database=myContext.getDatabasePath(DB_NAME);

        if (database.exists()) {

            Log.i("Database", "Found");

            String myPath = database.getAbsolutePath();

            Log.i("Database Path", myPath);

            checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

        } else {                

            // Database does not exist so copy it from assets here
            Log.i("Database", "Not Found");

        }

    } catch(SQLiteException e) {

        Log.i("Database", "Not Found");

    } finally {

        if(checkDB != null) {

            checkDB.close();

        }

    }

    return checkDB != null ? true : false;
}

No comments:

Post a Comment