Check Database exist fails android sqlite3openv2

I developed an android application where I shipped an sqlite database with the application. The Sqlite is initially stored at assets folder, and then copied to the proper directory (/data/data/[package]/databases/[db-name]) upon launch. I had the following piece of code for checking the existence of a database.

	public boolean databaseExist()
	{
		SQLiteDatabase checkDB = null;
		try {
			checkDB = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READONLY);
			checkDB.close();
			return true;
		}
		catch(SQLiteException e) {
			// Log an info message stating database doesn't exist.
		}
		
		return false;
	}

Even though I was catching an exception, however, I was still getting the error message (sqlite3_open_v2 failed):

sqlite3_open_v2("...',  &handle, 6, NULL) open failed

Doing some googling shows that its a popular problem.

Finally, I tried an old school java code with java.io.File, and then it seemed to work fine. So I modified the code like below:

public boolean databaseExist()
{
	File dbFile = new File(DB_PATH + DB_NAME);
	return dbFile.exists();
}

And, it works very well, I also noticed this appears to be somewhat faster than the previous openDatabase approach

I hope this helps someone out.

  • Robin

    Thanks. It helps.