I am trying to insert 1120 records (records=questions since it is trivia game) in my database but it is taking around 20secs i can\'t even work with insertHelper because it
I have used below method for around 1,00,000 rows insert and definitely faster than other. You can try it.
Instead of one bye one data insert, directly beginTransaction and insert all data and complete transaction.
Add below code(function) in DatabaseHelper(/DbHelper) class and call that function with arraylist of custom class(DataModel class).
Make some adding/changes as per your requirement:-
public void insertBigDataQuickly(ArrayList arrayList) {
SQLiteDatabase db = this.getWritableDatabase(); //db is instance of DatabaseHelper(/DBHelper) class
db.beginTransaction();
try {
String sql = "Insert or Replace into table_name (column1, column2, column3) values(?,?,?)";
SQLiteStatement statement = db.compileStatement(sql);
for (int i = 0; i < arrayList.size(); i++) { //Loop to insert all data one-by-one with Arraylist data
DataModel singleData = arrayList.get(i);
statement.bindString(1, singleData.getValue1()); //1 - Index value of column
statement.bindLong(2, singleData.getValue2()); //2 - Index value of column
statement.bindDouble(3, singleData.getValue3()); //3 - Index value of column
statement.executeInsert();
}
db.setTransactionSuccessful(); // This commits the transaction
}catch (Exception e) {
e.printStackTrace();
Log.d("Database error: ",e.getMessage());
}
finally {
db.endTransaction();
}
db.close();
}