sqlite数据库错误“没有这样的表”在Android P

Had*_*adi 0 sqlite android android-9.0-pie

我使用SQLiteOpenHelper从资产读取数据库。它适用于除android p以外的所有android版本。

这是我的copyDataBase函数:

 private static void copyDataBase() throws IOException {
        InputStream myInput = myContext.getAssets().open(DB_NAME);
        String outFileName = DB_PATH + DB_NAME;
        OutputStream myOutput = new FileOutputStream(outFileName);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer))>0){
            myOutput.write(buffer, 0, length);
        }
        myOutput.flush();
        myOutput.close();
        myInput.close();
        SharedPreferences shData = myContext.getSharedPreferences("data", 0);
        SharedPreferences.Editor editor = shData.edit();
        editor.putBoolean("databaseIsOnMemory", true);
        editor.commit();
    }
Run Code Online (Sandbox Code Playgroud)

我在这个函数中放了一个日志,它确实通过了它。但我收到此错误:

android.database.sqlite.SQLiteException: no such table: irancell (code 1 SQLITE_ERROR)
Run Code Online (Sandbox Code Playgroud)

这就是我如何获取数据:

c = myDataBase.query(true, Table_Name, new String[] {IDData,TextData},
    null,null,null,null,null, null);
c.moveToFirst();
int i = 1;
while(!c.isAfterLast()){
    list.add(new KharidBaste(c.getInt(0),c.getString(5)));
    c.moveToNext();
    i++;
}
Run Code Online (Sandbox Code Playgroud)

并且错误在行上:“ c.moveToFirst();”

小智 5

在createDB函数中,必须在this.getReadableDatabase()之后添加this.close()

this.getReadableDatabase();
this.close();
Run Code Online (Sandbox Code Playgroud)

此链接可以为您提供帮助