如何在SQLite中优化插入数千条记录

时间:2016-04-14 07:59:31

标签: android sqlite

我在数据库中使用提到的代码插入3000条记录我从服务器获取参数列表并使用它们进行查询但问题是在模拟器上插入2.5分钟并且在实际设备上只需要两倍。

所以请告诉我如何优化插入。

这是我的代码

public boolean insertRowDynamically(TableData tableData, String tableName) {

        keysStringBuilder.delete(0,keysStringBuilder.length());
        valuesStringbuilder.delete(0,valuesStringbuilder.length());
        SQLiteStatement sqLiteStatement = null;
        response = true;
        flag = true;
        first=true;
        LinkedHashMap<String,Object> values= new LinkedHashMap<>();
        List<Map<String, Object>> rowValue = tableData.getListofMapRecords();
        try {
            openDataBase();
            myDataBase = this.getWritableDatabase();
            myDataBase.beginTransactionNonExclusive();


            int i=0;
        for (Map<String, Object> map : rowValue) {

            values.clear();
            values.putAll(map);
            Set keys = values.keySet();
            Iterator<String> it = keys.iterator();




                    i=1;
                while (it.hasNext()) {
                    String key = it.next();
                    if (flag) {

                        keysStringBuilder.append(key);
                        valuesStringbuilder.append("?");

                        flag = false;
                    } else {
                        keysStringBuilder.append(", ").append(key);
                        valuesStringbuilder.append(", ").append("?");
                    }
                    if (!it.hasNext()) {
                        String query = "INSERT INTO " + tableName + "(" + keysStringBuilder + ") VALUES(" + valuesStringbuilder + ")";
                        sqLiteStatement = myDataBase.compileStatement(query);
                    }




                }




            }





            first = false;
                i=1;


            for ( String key :values.keySet())
            {
                sqLiteStatement.bindString(i,values.get(key).toString());
                i++;

            }


            sqLiteStatement.executeInsert();
            sqLiteStatement.clearBindings();











            } catch (SQLException e) {
                Log.d("Syncing Data", "From insertRowDynamically " + e);
            } finally {

                close();
            }


        return response;
    }

1 个答案:

答案 0 :(得分:0)

开始使用表级转换而不是行级事务,它就像魅力一样。

public boolean insertRowDynamically(TableData tableData, String tableName) {

        keysStringBuilder.delete(0,keysStringBuilder.length());
        valuesStringbuilder.delete(0, valuesStringbuilder.length());
        SQLiteStatement sqLiteStatement = null;
        response = true;
        flag = true;
        first=true;
        LinkedHashMap<String,Object> values= new LinkedHashMap<>();
        List<Map<String, Object>> rowValue = tableData.getListofMapRecords();
        try {
            openDataBase();
            myDataBase = this.getWritableDatabase();
            myDataBase.beginTransactionNonExclusive();
            int i=0;
            Log.d("Sync Time","Table Data Size "+rowValue.size());
        for (Map<String, Object> map : rowValue) {

            values.clear();
            values.putAll(map);
            Set keys = values.keySet();
            Iterator<String> it = keys.iterator();


            i = 1;
            if (first) {
                while (it.hasNext()) {
                    String key = it.next();
                    if (flag) {

                        keysStringBuilder.append(key);
                        valuesStringbuilder.append("?");

                        flag = false;
                    } else {
                        keysStringBuilder.append(", ").append(key);
                        valuesStringbuilder.append(", ").append("?");
                    }
                    if (!it.hasNext()) {
                        String query = "INSERT INTO " + tableName + "(" + keysStringBuilder + ") VALUES(" + valuesStringbuilder + ")";
                        sqLiteStatement = myDataBase.compileStatement(query);
                    }


                }


            }


            first = false;
            i=1;


            for ( String key :values.keySet())
            {
                sqLiteStatement.bindString(i,values.get(key).toString());
                i++;

            }


            sqLiteStatement.executeInsert();
            sqLiteStatement.clearBindings();

        }

            myDataBase.setTransactionSuccessful();
            myDataBase.endTransaction();

            } catch (SQLException e) {
                Log.d("Syncing Data", "From insertRowDynamically " + e);
            } finally {

                close();
            }


        return response;
    }