sarvesh chavan sarvesh chavan - 1 year ago 49
Android Question

Inserting Bulk data taking lot of time Sqlite

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 has been deprecated in android.
i searched a lot and used beginTransaction(),setTransactionSuccessful() & db.endTransaction(); but nothing helped. maybe i haven't used them correct so please correct me if it's wrong

HelperClass

private void addingeachquestions(Questions question) {
SQLiteDatabase db = this.getWritableDatabase();
db.beginTransaction();
try {
ContentValues values = new ContentValues();
values.put(QUESTION, question.getQUESTION());
values.put(OPTION1, question.getOPT1());
values.put(ANSWER, question.getANSWER());
values.put(ANSWER2, question.getANSWER2());
db = this.getWritableDatabase();
db.insert(TABLE_NAME, null, values);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}

public void addquestions() {
//famous people
Questions q1 = new Questions("Who was the first African American to have served as president of United States of America ?", "BAROBAACKMAQCAEMBD", "BARACK", "OBAMA");
this.addingeachquestions(q1);
Questions q2 = new Questions("Who co-founded Apple company with Steve Wozniak, Ronald Wayne ?", "TSOVWIBYUBZRGOEJSE", "STEVE", "JOBS");
this.addingeachquestions(q2);


MainActivityClass

demoHelperClass = new DemoHelperClass(this);
SQLiteDatabase sqLiteDatabase = demoHelperClass.getWritableDatabase();
demoHelperClass.addquestions();

Answer Source

You can collect your data, put it in a list, then iterate through the list inside a transaction like so:

private void addAllQuestions(Arraylist<Questions> allQuestions) {
    SQLiteDatabase db = this.getWritableDatabase();
    db.beginTransaction();
    try {
        ContentValues values = new ContentValues();

        for (Questions question: allQuestions) {
            values.put(QUESTION, question.getQUESTION());
            values.put(OPTION1, question.getOPT1());
            values.put(ANSWER, question.getANSWER());
            values.put(ANSWER2, question.getANSWER2());
            db = this.getWritableDatabase();
            db.insert(TABLE_NAME, null, values);        
        }

        db.setTransactionSuccessful();

    } finally {
        db.endTransaction();
    }
}

public void addquestions() {
    //famous people
    ArrayList<Questions> allQuestions = new ArrayList<Questions>();

    allQuestions.append(new Questions("Who was the first African American to have served as president of United States of America ?", "BAROBAACKMAQCAEMBD", "BARACK", "OBAMA"));

    allQuestions.append(new Questions("Who co-founded Apple company with Steve Wozniak, Ronald Wayne ?", "TSOVWIBYUBZRGOEJSE", "STEVE", "JOBS"));

    this.addAllQuestions(allQuestions);
}

Based on this: https://stackoverflow.com/a/32088155/4268599

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download