I'm using SQLiteOpenHelper
in my Android app and would like to insert ~20k statements in the onCreate
method. I can't use a prefilled database as then I'm facing problems with OnePlus devices (SQLiteAssetHelper - problems on specific phones, e.g. OnePlus).
Now I stored my insert-statemtents in a file and use import it the following way: (R.raw.prefilled_data
is the file containing the 20k statements).
Currently it takes ~ 6 seconds on a Nexus 5, which is not too bad, but maybe there's a way to make this even faster?
this 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();
}