如何提高SQLite性能?

时间:2014-10-30 13:52:39

标签: android performance sqlite

我想在android SQLite中插入超过3000条记录,但以下代码需要花费很多时间来处理数据 这是我的代码。

public boolean addSale(Sale objSale) {
    ContentValues values = new ContentValues();
    values.put(SALE_BRANCH, objSale.getBranch());
    values.put(SALE_SUPPLIER, objSale.getSupplier());
    values.put(SALE_BUYER, objSale.getBuyer());
    values.put(SALE_CAT1, objSale.getCat1());
    values.put(SALE_CAT2, objSale.getCat2());
    values.put(SALE_CAT3, objSale.getCat3());
    values.put(SALE_CAT4, objSale.getCat4());
    values.put(SALE_CAT5, objSale.getCat5());
    values.put(SALE_CAT6, objSale.getCat6());
    values.put(SALE_DESIGNO, objSale.getDesigNo());
    values.put(SALE_ITEMSIZE, objSale.getItemSize());
    values.put(SALE_SALEQTY, objSale.getSaleQty());
    values.put(SALE_STOCKQTY, objSale.getStockQty());
    values.put(SALE_FinalProduct, objSale.getFinalProduct());
    values.put(SALE_PriceRange, objSale.getPriceRange());
    values.put(SALE_CoreNonCore, objSale.getCoreNonCore());
    values.put(SALE_Color, objSale.getColor());
    values.put(SALE_GSLCode, objSale.getGSLCode());
    values.put(SALE_Wanted, objSale.getWanted());
    values.put(SALE_Pqty, objSale.getPqty());
    values.put(SALE_MRP, objSale.getMRP());
    values.put(SALE_PRate, objSale.getPRate());
    SQLiteDatabase db = this.getWritableDatabase(); 
    db.insert(TABLE_SALE, null, values);
    db.close();
    return true;
}

这是我的Asynk任务。我从webservice获取数据并插入SQLite

protected void onPostExecute(String result) 
        {
            try {

                String l = result.replace("\\", "");
                l = l.replace("''", "");
                String sdsd = l.substring(1, l.length() - 1);
                JSONArray jsonArray = new JSONArray(sdsd);
                Log.i("JSON", "Number of surveys in feed: " +jsonArray.length());
                /*if(db.delSaleData()){

                }*/
                    for (int i = 0; i < jsonArray.length(); i++) 
                    {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                            sl.setBranch(jsonObject.getString("Branch"));
                            sl.setSupplier(jsonObject.getString("Supplier"));
                            sl.setBuyer(jsonObject.getString("Buyer"));
                            sl.setCat1(jsonObject.getString("Cat1"));
                            sl.setCat2(jsonObject.getString("Cat2"));
                            sl.setCat3(jsonObject.getString("Cat3"));
                            sl.setCat4(jsonObject.getString("Cat4"));
                            sl.setCat5(jsonObject.getString("Cat5"));
                            sl.setCat6(jsonObject.getString("Cat6"));
                            sl.setDesigNo(jsonObject.getString("DesigNo"));
                            sl.setItemSize(jsonObject.getString("ItemSize"));
                            sl.setSaleQty(jsonObject.getString("SaleQty"));
                            sl.setStockQty(jsonObject.getString("StockQty"));
                            sl.setFinalProduct(jsonObject.getString("FinalProduct"));
                            sl.setPriceRange(jsonObject.getString("PriceRange"));
                            sl.setCoreNonCore(jsonObject.getString("CoreNonCore"));
                            sl.setColor(jsonObject.getString("Color"));
                            sl.setGSLCode(jsonObject.getString("GSLCode"));
                            sl.setWanted(jsonObject.getString("Wanted"));
                            sl.setPqty(jsonObject.getString("Pqty"));
                            sl.setMRP(jsonObject.getString("MRP"));
                            sl.setPRate(jsonObject.getString("PRate"));

                            if(db.addSale(sl))
                            {
                                //Toast.makeText(getApplicationContext(), " Insert.." , Toast.LENGTH_SHORT).show(); 
                            }
                    }

                    setData();


                } catch (Exception e) {
                    e.printStackTrace();
            }
            dialog.dismiss();
            setTableData("All");


        }

3 个答案:

答案 0 :(得分:2)

使用SQLiteStatement例如:

private void bulkInsertRecords(String[] records) {
          String sql = "INSERT INTO "+ SAMPLE_TABLE_NAME +" VALUES (?,?,?);";
          SQLiteStatement statement = sampleDB.compileStatement(sql); //Este é o prepare
          sampleDB.beginTransaction();
          for (int i = 0; i<records.length; i++) {
                    statement.clearBindings();
                    statement.bindString(1, records[0]);
                    statement.bindString(2, records[1]);
                    statement.bindString(3, records[2]);
                    statement.execute();
           }
           sampleDB.setTransactionSuccessful(); 
           sampleDB.endTransaction();
} 

答案 1 :(得分:1)

每次要插入时都不要打开和关闭数据库。打开一次,然后在退出应用程序时将其关闭。

答案 2 :(得分:-1)

使用Async课程。执行操作onBackground,而不是在主UI线程中。

更新

在您的代码中,您通过onPost插入SQLite,而不是onBackgroundonPost在UI线程上执行操作,而onBackground在单独的(后台)线程上执行操作,因此它不会影响UI。因此操作代码移至onBackground