简体   繁体   English

Android:SQLite游标错误的数据库大小超过1 MB

[英]Android: SQLite cursor error above 1 MB of database size

I am developing application using the SQLite database. 我正在使用SQLite数据库开发应用程序。 I have the following code for getting the database all the values. 我有以下代码来获取数据库的所有值。

ArrayList<String> values=new ArrayList<String>(); 
String[] ColumnNames;
String selectQuery="SELECT * FROM "+tableName;
Cursor cursor=sDataBase.rawQuery(selectQuery, null);
if(cursor!=null && cursor.getColumnCount()>0 && cursor.getCount()>0){
    ColumnNames=cursor.getColumnNames();
    if (cursor.moveToFirst()){
        do{
            String value="";
            for(int i=0;i<cursor.getColumnCount();i++){
                if(value!="")
                    value=value +", \\\""+ColumnNames[i]+"\\\":\\\"" + cursor.getString(i)+"\\\"";
                else
                    value= "\\\""+ ColumnNames[i] +"\\\":\\\"" + cursor.getString(i) +"\\\"";
            }
            value="{" + value + "}";
            values.add(value);
        }while(cursor.moveToNext());
    }
    cursor.close();
}

If the database size is more than 1MB, the app is getting crashed. 如果数据库大小大于1MB,则该应用程序将崩溃。 How can I get the database values more than 1 MB.? 如何获得超过1 MB的数据库值?

EDIT1: 编辑1:

Logcat values: Logcat值:

10-10 14:46:24.863: E/dalvikvm-heap(3248): Out of memory on a 6612888-byte allocation.

EDIT2: 编辑2:

Code using the stringbuffer 使用字符串缓冲区的代码

   if(cursor!=null && cursor.getColumnCount()>0 && cursor.getCount()>0)
            {
                ColumnNames=cursor.getColumnNames();
                if (cursor.moveToFirst()){
                       do{
                           StringBuffer value= new StringBuffer();
                            value.append("{");
                           for(int i=0;i<cursor.getColumnCount();i++)
                           {
                               if(value.length()>1)
                                   value.append(", \\\""+ColumnNames[i]+"\\\":\\\"" + cursor.getString(i)+"\\\"");
                               else
                                  value.append("\\\""+ ColumnNames[i] +"\\\":\\\"" + cursor.getString(i) +"\\\"");
                           }
                           value.append(value + "}");
                           values.add(value);
                       }while(cursor.moveToNext());
                    }
                    cursor.close();
            }

Length of the value StringBuffer is 4860024. 值StringBuffer的长度为4860024。

Well, it's clear that you're just running out of memory. 好吧,很明显,您的内存即将耗尽。 You are building up a String representation of the database using a series of immutable String objects. 您正在使用一系列不可变的String对象构建数据库的String表示形式。

String value="";

Every time you do a string concatenation, value=value +", \\\\\\"" you are creating a new String object. This is very inefficient in terms of memory usage. 每次执行字符串连接时, value=value +", \\\\\\""都会创建一个新的String对象。这在内存使用方面非常低效。

Try using a StringBuilder instead of a String , and use its append() method instead of the string concatenation operator + . 尝试使用StringBuilder代替String ,并使用其append()方法代替字符串连接运算符+ This will allow the string to be built up in a single buffer (which will grow as needed), which makes it much more likely to fit in the available heap memory. 这将允许在单个缓冲区中构建字符串(该字符串将根据需要增长),从而使其更有可能放入可用的堆内存中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM