本文介绍了插入大量数据需要花费大量时间Sqlite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正尝试在数据库中插入1120条记录(records = questions,因为它是琐事游戏),但是这大约需要20秒,我什至无法使用insertHelper,因为它已在android中弃用.我进行了大量搜索,并使用了 beginTransaction() setTransactionSuccessful()和& db.endTransaction(); ,但没有任何帮助.也许我没有正确使用它们,所以如果不对,请纠正我

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

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

MainActivityClass

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

推荐答案

您可以收集数据,将其放入列表中,然后在事务内遍历列表,如下所示:

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);
}

基于此: https://stackoverflow.com/a/32088155/4268599

这篇关于插入大量数据需要花费大量时间Sqlite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 06:02