Getting an SQLiteCantOpenDatabaseException
android, android-sqlite, sqlite
Solution
I always follow below step to copy my database from assets folder:
private void copyDatabase() throws IOException{
InputStream inputStream = context.getAssets().open(DB_NAME);
String dbCreatePath = DB_PATH+DB_NAME;
OutputStream outputStream = new FileOutputStream(dbCreatePath);
byte[] buffer = new byte[1024];
int length;
while((length = inputStream.read(buffer))>0){
outputStream.write(buffer,0,length);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
Once it is copied i will check like below :
private boolean checkDatabase(){
SQLiteDatabase checkDB = null;
try {
String dbPath = DB_PATH+DB_NAME;
checkDB = SQLiteDatabase.openDatabase(dbPath, null,
SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
// TODO: handle exception
}
if(checkDB!=null){
checkDB.close();}
return checkDB != null ? true:false;
}
Problem
In my application, I have a copy of an SQLite database in my assets folder. As far as I know, it's working fine. When my application installs for the first time in my emulator, I get an error like so: ``` Failed to open the database. Closing it. android.database.sqlite.SQLiteCantOpenDatabaseException: unable to open database file E/SQLiteDatabase(7516): at android.database.sqlite.SQLiteDatabase.dbopen(Native Method) E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:1013) E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:986) E/SQLiteDatabase(29308): at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:962) E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.checkDBExists(URLDatabaseHelper.java:86) E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.createURLDB(URLDatabaseHelper.java:54) E/SQLiteDatabase(29308): at com.guayama.database.URLDatabaseHelper.<init>(URLDatabaseHelper.java:38) ``` Here is my initial code to open the database: ``` SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.CREATE_IF_NECESSARY); ``` Here is another method that I tried: ``` checkDB = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.OPEN_READONLY); ``` I also tried using this: ``` SQLiteDatabase.openDatabase(mPath, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS); ``` Any suggestions on how to solve this problem?