0

im using SQLiteOpenHelper to read database from assets . its working on all android versions but android p .

this is my copyDataBase function :

 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();
    }

i puted a log into this function and it did go through it . but im getting this error :

android.database.sqlite.SQLiteException: no such table: irancell (code 1 SQLITE_ERROR)

this is how i get data :

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++;
}

and the error is on line : "c.moveToFirst();"

Hadi Ahmadi
  • 129
  • 12
  • Possible duplicate of [Android P - 'SQLite: No Such Table Error' after copying database from assets](https://stackoverflow.com/questions/50476782/android-p-sqlite-no-such-table-error-after-copying-database-from-assets) – rmtheis Jun 05 '18 at 16:14
  • @hadi I am also getting the same type of error.Does it solved? – Mujammil Ahamed Oct 16 '18 at 09:29

2 Answers2

6

In your createDB function you must add this.close() after this.getReadableDatabase()

this.getReadableDatabase();
this.close();

This link can help you

Shahab Rahnama
  • 982
  • 1
  • 7
  • 14
-2

Check the table name in your database. It may be different. And remember table name is case sensitive. For example, table name Employee and employee is not same. So check table name.

Parth Suthar
  • 123
  • 4
  • I did it before . as i said its working on other android versions . for example im running it on android oreo with same table name and its working properly but on android P . its crashing – Hadi Ahmadi Jun 01 '18 at 08:34
  • 2
    *And remember table name is case sensitive.* **not true** *For example, table name Employee and employee is not same.* **bs, in sql it is the same** – Selvin Jun 01 '18 at 08:34