Android在4,2错误时为text / blob分配223605个字节失败

时间:2014-01-24 05:38:54

标签: android database sqlite cursor

我无法理解错误的含义是什么?我将大数据动态存储到数据库中.i存储五个raw并删除第一个raw后添加最新的第六个。我的代码在Nexus 7.0和其他设备上工作正常,但错误显示在小型设备或法律存储设备中。

错误

 01-24 11:04:35.927: E/CursorWindow(30681): need to grow: mSize = 1048576, size = 340236, freeSpace() = 225346, numRows = 5
 01-24 11:04:35.927: E/CursorWindow(30681): not growing since there are already 5 row(s), max size 1048576
 01-24 11:04:35.927: E/Cursor(30681): Failed allocating 340236 bytes for text/blob at 4,2

           //this is for reading the raw data from the database - updated

databse = helper.getReadableDatabase();
Cursor  cursor = databse.rawQuery("select * from comments where newspaper = " + newspaper,null);
            String quantity_tb = null ;
            try 
            {                   
                while (cursor.moveToNext()) 
                {
                    String name_tb = cursor.getString(0);
                    String price_tb = cursor.getString(1);
                    quantity_tb = cursor.getString(2);
                         }

            }
            finally
            {
                 if(cursor != null && !cursor.isClosed()){
                        cursor.close();
                    } 

                databse.close();
            }       
   ////////////

            databse = helper.getReadableDatabase();
        Cursor cursor= databse.rawQuery("Select count(*) from comments;", null);
        cursor.moveToFirst();
        int countdb= cursor.getInt(0);
        cursor.close();
        databse.close();


        if (countdb == 5) 
        {   
            deletefirstraw();
        }

        if (!masterData.isEmpty()) {

            databse = helper.getWritableDatabase();
            ContentValues cv = new ContentValues();
            cv.put(helper.NEWSPAPER_NAME,masterData.get(0).newspaper);//name
            cv.put(helper.NEWSPAPER_NEWS,json);// large data
            databse.insert(helper.NEWSPAPER, null, cv);
            databse.close();
        }
    }
}

public void deletefirstraw() {
    // TODO Auto-generated method stub

          databse = helper.getWritableDatabase();
            Cursor cursor1 = databse.query(helper.NEWSPAPER, null, null, null, null, null, null); 
                if(cursor1.moveToFirst()) {
                    String rowId = cursor1.getString(cursor1.getColumnIndex(helper.COLUMN_ID)); 

                    databse.delete(helper.NEWSPAPER, helper.COLUMN_ID + "=?",  new String[]{rowId});
                   }
          cursor1.close();
        databse.close();

}

1 个答案:

答案 0 :(得分:0)

首先,如果你打开和关闭你的数据库,你的性能会受到很大影响。为一个操作集保留对它的引用。

其次,关闭数据库时,将其放入finally块以确保调用它。

第三,如果未输入if (!masterData.isEmpty()) {块,则DB不会关闭。

您也可以发布更多的堆栈跟踪,以便我们可以看到错误发生的位置。