Sqlite Issues with OnePlus device

2019-08-07 20:04发布

问题:

We have an android app that uses SQLite for some form of persistent storage. The sqlite code works on all devices except Oneplus devices. ( I am testing on Oneplus 2 A2003)

Following is the error that I am seeing in logcat.

android.database.sqlite.SQLiteException: no such table: testtable (code 1): , while compiling: INSERT INTO testtable(ID,CompletedOn,CreatedOn,Type,Pending,Priority,Attributes) VALUES (?,?,?,?,?,?,?)

Following is the piece of database that is used for open database

   SQLiteDatabase db = SQLiteDatabase.openDatabase(this.getHelperContext().getDatabasePath(DATABASE_NAME).getAbsolutePath(), null,
                    SQLiteDatabase.NO_LOCALIZED_COLLATORS);

Have tried even specifying the access rights while opening, but no difference. e.g. SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE

Any help would be appreciated.

回答1:

Check this answer https://stackoverflow.com/a/33032467/4504324 it solved the issue for me.

Just close the db before copying the sqlite stream into internal database.

private void copyDataBase() throws IOException {

    try {
        //Open your local db as the input stream
        InputStream myInput = mContext.getAssets().open("databases/" + DB_NAME+".sqlite");
        // Path to the just created empty db
        String outFileName = mContext.getDatabasePath(DB_NAME).getAbsolutePath();

        //Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        //close the database so that stream can be copied
        this.getReadableDatabase().close();

        //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();
    } catch (Exception e) {
        e.printStackTrace();
    }
}