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 st
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:
Go to the directory, where you have the database, and launch SQLite:
sqlite3 yourfile.db
You can check the database integrity by:
pragma integrity_check;
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
Export your data to an sql file:
echo .dump | sqlite3.exe yourdbname.db > yourdbname.sql
Change file name mv yourdbname.db yourdbname.db.original
Create a new database with your sql.
sqlite3.exe -init yourdbname.sql yourdbname.db
Open the new file with sqlite browser, and see what happens!
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:
private boolean checkDataBase(){
File dbFile = new File( DATABASE_PATH, DATABASE_NAME );
return dbFile.exists();
}
Change createDb method:
public void createDataBase() {
if(checkDb()){
//do nothing
} else {
copyDatabase();
}
}
Change copyDatabase() method:
private void copyDataBase(){
getReadableDatabase();
//add the rest of your current implementation
}
Change accessibility of your constructor to private and add these methods:
private DBHelper mInstance = null;
public static DBHelper getInstance(Context context) {
if(mInstance == null)
mInstance = new DBHelper(context)
return mInstance;
}
//remove the old openDatabase method
public SQLiteDatabase openDatabase() {
return getReadableDatabase(); //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...
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
String outFileName =myContext.getDatabasePath(DB_NAME).getPath() ;
and that's the real valid location for your file. so , your copyDataBase() will be like this
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = context.getAssets().open("db/" + DATABASE_NAME);
// Path to the just created empty db
String outFileName =myContext.getDatabasePath(DB_NAME).getPath() ;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[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()
try this
db = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);