2

I have to check whether a database exists or not in android?? how can i check? It should return a boolean value after checking?plz provide me with the code.

1 Answer 1

1

Something like:

private boolean databaseExists() {
    SQLiteDatabase checkDB = null;
    try {
        String myPath = DB_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null,
            SQLiteDatabase.OPEN_READONLY);
        if(checkDB != null){
            checkDB.close();
            return true;
        } else {
            //database does not exist
            return false;
        }
    } catch (SQLiteException e) {
        //database does not exist
        return false;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

And here DB_PATH should be like this: "/data/data/YOUR_PACKAGE_NAME_IN_MANIFEST/databases/".
Yes, and should be constructed from your Context - context.getApplicationInfo().dataDir + "/databases/"
thank you!! now i am able to check for the database . the code was really very useful for me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.