SQLiteOpenHelper: the fastest way to execute ~ 20k statements?

I use SQLiteOpenHelperin my Android application and would like to insert ~ 20k statements into the method onCreate. I cannot use a pre-populated database, as I have to deal with problems with OnePlus devices ( SQLiteAssetHelper - problems on certain phones, for example OnePlus ).

Now I saved my statemtents inserts in a file and imported it as follows: ( R.raw.prefilled_datais a file containing 20k instructions).

Nexus 5 currently requires ~ 6 seconds, which isn’t so bad, but maybe there’s a way to do it even faster?

it happens inside onCreate

    try {
        insertFromFile(R.raw.prefilled_data);
    } catch (IOException e) {
        e.printStackTrace();
    }

.

    private void insertFromFile(int resourceId) throws IOException {
        // Open the resource
        InputStream insertsStream = AboalarmApp.getContext().getResources().openRawResource(resourceId);
        BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));

        // Iterate through lines (assuming each insert has its own line and theres no other stuff)
        while (insertReader.ready()) {
            String insertStmt = insertReader.readLine();
            myDataBase.execSQL(insertStmt);
        }
        insertReader.close();
    }
+4

Source: https://habr.com/ru/post/1624300/


All Articles