【问题标题】:Inserting Bulk data taking lot of time Sqlite插入批量数据需要很多时间 Sqlite
【发布时间】:2017-05-23 03:00:30
【问题描述】:

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

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

主活动类

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

【问题讨论】:

  • addquestions()addingeachquestions(Questions question) ?
  • 将所有插入放入一个事务中。在此处查看接受的答案:stackoverflow.com/questions/32088056/…
  • 这是您必须在应用程序中执行的操作吗?您不能将预填充的数据库与您的应用一起打包吗?
  • @John Joe 我已经更新了问题,请检查!!!
  • @aljo 我之前已经扔掉了,但我不明白他们是如何在内容值之后使用循环的。你能告诉我它是如何工作的吗

标签: android database sqlite


【解决方案1】:

使用以下方法插入了大约 1,00,000 行,并且绝对比其他方法更快。你可以试试。

直接beginTransaction而不是一一插入数据,插入所有数据,完成事务。

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

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

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

【讨论】:

    【解决方案2】:

    你可以收集你的数据,把它放在一个列表中,然后像这样在一个事务中遍历这个列表:

    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

    【讨论】:

      猜你喜欢
      • 2021-07-24
      • 2017-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多