3

I have a database file in assert/ folder, and i used code for copying database. But after copying it to the device the table is missing. This is happening only if i run application in Android P(8.1) and its working fine with other android version i have checked on Nougat as well and it is working fine.

Please help if any new update is there for Android P..??

Code for Copy database:

public void createDataBase() throws IOException {

    boolean dbExist = checkDataBase();

    if (dbExist) {
        //do nothing - database already exist            
    } else {

             this.getReadableDatabase();

        try {

            copyDataBase();

        } catch (IOException e) {

            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase() {

    SQLiteDatabase checkDB = null;

    try {
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

    } catch (SQLiteException e) {
        //database does't exist yet.
        e.printStackTrace();
    }

    if (checkDB != null) {

        checkDB.close();

    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException {

    InputStream myInput = myContext.getAssets().open(DB_NAME);
    String outFileName = DB_PATH + DB_NAME;

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

}
  • You can use https://github.com/jgilfelt/android-sqlite-asset-helper – Xenolion May 01 '18 at 12:26
  • 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 02 '18 at 14:25
  • @rmtheis : The link you have provide is a duplicate.. because this question was asked before as that link question. – Gaurav Jain Aug 07 '18 at 05:48
  • True, you asked your question first! But the other question has an accepted answer, uses better grammar and spelling, and has a more concise code example. – rmtheis Aug 11 '18 at 14:32
  • Did you get any solution? because I am facing the same issue with Android Q. – N_J May 07 '19 at 11:25

3 Answers3

3

I solved this issue in android P by adding this.close() after this.getReadableDatabase()

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

I hope this solution will be useful to you.

Shahab Rahnama
  • 982
  • 1
  • 7
  • 14
0

To solve this, try below code in SQLiteHelper's onUpgrade():

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if(newVersion>oldVersion)
  copyDatabase();
}
Janvi Vyas
  • 732
  • 5
  • 16
0
private final static String DB_PATH = "/data/data/packageName/databases/";

DbHelper.java

//Database HelperClass Constuctor
public DBHelper(Context context)
{
        super(context, "db.sqlite", null, 1);
        Log.e(TAG, "DBHelper: construction");
        this.context = context;
        dbFile = new File(DB_PATH + dbName);
        if (!dbFile.exists()) 
        {
            SQLiteDatabase db = super.getReadableDatabase();
            db.close();
            copyDataBase(db.getPath());
        }    
 }

close the databse by calling this.close(); after calling getReadableDatabase();

Joker
  • 153
  • 14