插入批量数据需要花费大量时间Sqlite

时间:2017-05-23 03:00:30

标签: android database sqlite

我正在尝试在我的数据库中插入1120条记录(记录=问题,因为它是琐事游戏),但它需要大约20秒,我甚至无法使用insertHelper,因为它已在android中被弃用。 我经常搜索并使用 beginTransaction() setTransactionSuccessful()& db.endTransaction(); 但没有任何帮助。也许我没有正确使用它们,所以请纠正我,如果它是错的

助手类

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

2 个答案:

答案 0 :(得分:1)

已使用以下方法 1,00,000行插入,并且肯定比其他更快。你可以尝试一下。

而不是一个单独的数据插入,直接beginTransaction并插入所有数据并完成事务。

DatabaseHelper(/ DbHelper)类中添加以下代码(函数),并使用自定义类(DataModel类)的arraylist调用该函数。

根据您的要求进行一些添加/更改: -

public void insertBigDataQuickly(ArrayList<DataModel> 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();
}

答案 1 :(得分:0)

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

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