SQLite: No Such Table Error Android P problem
Arg finally solved it adding this.close();
after this.getReadableDatabase();
That openned connection was generating the original no such table exception
So I used Util.getDatabasePath(DB_NAME); instead of the complex propossed solution in Android P - 'SQLite: No Such Table Error' after copying database from assets and now the code is much more simpler
Thanks a lot to @LifeStyle who found the real problem.
Now the code is much more simpler:
public static String getDatabasePath(String fileNname){
return ApplicationContextProvider.getContext().getDatabasePath(fileNname).getAbsolutePath();
}
public class DbHelper extends SQLiteOpenHelper{
private String DB_NAME;
private String DB_COMPLETE_PATH;
private SQLiteDatabase mDataBase;
private static final int DATABASE_VERSION = 1;
public DbHelper(String name) throws IOException {
super(ApplicationContextProvider.getContext(), name+".db", null, DATABASE_VERSION);
this.DB_NAME = name+".db";
this.DB_COMPLETE_PATH = Util.getDatabasePath(DB_NAME);
if (checkIfDBExists()==false){
createDataBase();
}
openDataBase();
}
private void createDataBase() throws IOException {
this.getReadableDatabase();
this.close();
try {
copyDataBase();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private boolean checkIfDBExists() {
File dbFile = new File(DB_COMPLETE_PATH);
return dbFile.exists();
}
}