SQLite: No Such Table Error Android P problem

﹥>﹥吖頭↗ 提交于 2019-12-05 12:00:45

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 because thanks to him I 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();
    }
}

In your DBHelper class, you are creating the DB before you testing the if DB exists in method Util.getDatabasePath(DB_NAME). This line creating the DB

this.DB_COMPLETE_PATH = Util.getDatabasePath(DB_NAME);

By calling this method You sql open helper creates the databse(in your case empty because in OnCreate() method notthing happens).

If you are storing your DB in default app's db path then you can check if DB exist by next snippet:

File f = context.getDatabasePath("my_database.db");
if(f.exists()) {
   //Your code if DB exist
} else {
   //Your code if DB doesn't exist
}

And the better place to disable WAL is onConfigure() method in SQLiteOpenHelper

Nurdiyor Microstar

add this path your database

String dbPath = "/data/data/" + mContext.getPackageName() + "/cache/" + mDataBaseName;

Actually i have taken object of SQLiteDatabase as static so i was getting this issue.

aaru

My issues with Android P got solved by adding this.close(); after this.getReadableDatabase();

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!