Skip to content Skip to sidebar Skip to footer

Failed To Open Database/ Failed To Change Locale For (database) To 'en_us'

I have read solution from Failed to change locale for db '/data/data/my.easymedi.controller/databases/EasyMediInfo.db' to 'en_US' but it doesnt help me. I still have the same error

Solution 1:

I couldn't put all these codes in the comments, so please try these: Keep in mind that I am not adding all the boilerplates... ok?

Change checkDb method:

privatebooleancheckDataBase(){
     FiledbFile=newFile( DATABASE_PATH, DATABASE_NAME );
     return dbFile.exists();
}

Change createDb method:

public void createDataBase() {
    if(checkDb()){
        //do nothing
    } else {
        copyDatabase();
    }
}

Change copyDatabase() method:

privatevoidcopyDataBase(){
      getReadableDatabase();

      //add the rest of your current implementation

}

Change accessibility of your constructor to private and add these methods:

privateDBHelper mInstance = null;
publicstaticDBHelpergetInstance(Context context) {
   if(mInstance == null)
        mInstance = newDBHelper(context)
   return mInstance;
}
//remove the old openDatabase methodpublicSQLiteDatabaseopenDatabase() {
   returngetReadableDatabase(); //or you can use getWritableDatabase();
}

Besides I noticed that your db version in the posted code is 18 while in the logs that you posted it's saying 5 or 6? Can you post the latest logs...

Solution 2:

The problem that you get is that the file cannot be opened. Probably its something wrong with your path (it could be slightly different between diferent android versions), and anyways, you should never hardcode it.

You can get database path with context.getDatabasePath(); you pass the desired name to the file (no matter if it exists or not). And actually you are supposed to do it that way

For that, at any place you are using something like String outFileName = DB_PATH + DB_NAME; you should change it for

StringoutFileName=myContext.getDatabasePath(DB_NAME).getPath() ;

and that's the real valid location for your file. so , your copyDataBase() will be like this

privatevoidcopyDataBase()throws IOException {

    // Open your local db as the input streamInputStreammyInput= context.getAssets().open("db/" + DATABASE_NAME);

    // Path to the just created empty dbStringoutFileName=myContext.getDatabasePath(DB_NAME).getPath() ;

    // Open the empty db as the output streamOutputStreammyOutput=newFileOutputStream(outFileName);

    // transfer bytes from the inputfile to the outputfilebyte[] buffer = newbyte[1024];
    int length;
    while ((length = myInput.read(buffer)) > 0) {
        myOutput.write(buffer, 0, length);
    }

    // Close the streams
    myOutput.flush();
    myOutput.close();
    myInput.close();

}

and the same change in your private boolean checkDataBase()

Solution 3:

Probably the database file is actually corrupted. You can try to open it with sqlitebrowser

Also, you can do some other checks and try to restore it in case it is corrupted, if you install sqlite in your computer. You can follow this steps:

  1. Get sqlite here and install it
  2. Open command a add path to sqlite oyour environnment variable path to allow you to launch sqlite commands everywhere
  3. Go to the directory, where you have the database, and launch SQLite:

    sqlite3 yourfile.db
    
  4. You can check the database integrity by:

    pragma integrity_check;
    
  5. If it says that the file is ok, then we are lost! If it is corrupted, lets try to restore it.

Sometimes the best solution for a corrupted sqlite3 database is simple yet effective: Dump and Restore

  1. Export your data to an sql file:

    echo .dump | sqlite3.exe yourdbname.db > yourdbname.sql
    
  2. Change file name mv yourdbname.db yourdbname.db.original

  3. Create a new database with your sql.

    sqlite3.exe -init yourdbname.sql yourdbname.db
    
  4. Open the new file with sqlite browser, and see what happens!

Solution 4:

try this

db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);

Post a Comment for "Failed To Open Database/ Failed To Change Locale For (database) To 'en_us'"