4

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?

Community
  • 1
  • 1
SuReSh PaTi
  • 1,373
  • 7
  • 28
  • 46

2 Answers2

2

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;

}
Suave Nti
  • 3,721
  • 11
  • 54
  • 78
  • i got this exception after adding ur code SQLiteCantOpenDatabaseException: unable to open database file – SuReSh PaTi Jun 22 '12 at 14:32
  • please check it once i have added log – SuReSh PaTi Jun 22 '12 at 15:02
  • Did you tried calling createDatabase() before openDatabase(). like this: URLDatabaseHelper dbHelper = new URLDatabaseHelper(getApplicationContext()); dbHelper.createDatabase(); dbHelper.openDatabase(); – Suave Nti Jun 22 '12 at 15:22
  • i got error on checkDB = SQLiteDatabase.openDatabase(dbPath,null, SQLiteDatabase.OPEN_READWRITE); – SuReSh PaTi Jun 22 '12 at 15:29
  • yes, i got the same error on checkDB = SQLiteDatabase.openDatabase(dbPath,null, SQLiteDatabase.OPEN_READWRITE); and my db path is "/data/data//databases/db.s3db" – fargath Jul 04 '12 at 05:45
  • im getting this error especially in Android honeycomb, in other versions it works perfect. – fargath Jul 04 '12 at 05:53
  • @fargath : Its better you post your own question if you are facing any problems. – Suave Nti Jul 04 '12 at 08:01
  • Im having the same problem which described above, so i don want to create a duplicate question. – fargath Jul 04 '12 at 10:33
1

I'm having a similar problem, the difference being that the database works in my Eclipse Nexus 7 emulator but not on the Nexus 7 itself. I downloaded Questoid to be able to see what's in my database after I copy it from the assets folder but for some time the database did not even show up in File Explorer. I kept on doing the copy and finally it showed up. Having gone through the same process several times, I have not yet been able to figure out what is the common denominator, but repetition seems to be the key in my case. Now I still have to figure out why my Nexus 7 itself can't open the database when I install the app on the device. I experience the same exception, but only on the device itself.

bobv
  • 41
  • 7