Android SQLite数据库无法选择值较大的行,引发IllegalStateException

Android SQLite数据库无法选择值较大的行,引发IllegalStateException,android,sqlite,Android,Sqlite,我创建了下面的类,用于将数据保存到SQLite数据库。正如您所看到的,它只是一个键/值映射。键是一个文本字段,值是一个BLOB 除了一种情况外,它工作正常: 我使用“add”来添加一个长度约为2500000个字符的字符串。事实上,它是一个JSON编码的字符串(我还尝试将javascript样式的encodeURIComponent应用于该字符串,以确保没有非法字符干扰) 在这个场景中,成功地添加了值(没有抛出错误,add()返回true)。但是,当我随后立即调用get(key)时,将从curso

我创建了下面的类,用于将数据保存到SQLite数据库。正如您所看到的,它只是一个键/值映射。键是一个文本字段,值是一个BLOB

除了一种情况外,它工作正常: 我使用“add”来添加一个长度约为2500000个字符的字符串。事实上,它是一个JSON编码的字符串(我还尝试将javascript样式的encodeURIComponent应用于该字符串,以确保没有非法字符干扰)

在这个场景中,成功地添加了值(没有抛出错误,add()返回true)。但是,当我随后立即调用get(key)时,将从cursor.getString(0)抛出IllegalStateException

具体而言:

"Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it." (id=830026207728) 
我已经检查了cursor对象,没有发现任何问题(mCount==1,mColumns==String[1],等等)


经过进一步调查,控制台窗口告诉我“光标窗口”已满,例如,它没有空间。请参阅此线程:

我的解决方案是“分块”数据。例如,“add”函数检测字符串是否>固定长度,在这种情况下,它为字符串存储多个条目(使用许多行)。然后,“get”函数检测到这一点并重新组合块

class DatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 2;

    // Contacts Table Columns names
    private static final String KEY_KEY = "key";
    private static final String KEY_VAL = "value";

    public DatabaseHandler(Context context) {
        super(context, Defines.DATA_PREFIX, null, DATABASE_VERSION);
    }

    public String getTableName() {
        return "storage";
    }

    // Creating Tables
    @Override
    public void onCreate(SQLiteDatabase db) {
        String q = "CREATE TABLE " + this.getTableName() + "(" + KEY_KEY + " TEXT PRIMARY KEY," + KEY_VAL + " BLOB" + ")";
        db.execSQL(q);
    }

    // Upgrading database
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // Drop older table if existed
        db.execSQL("DROP TABLE IF EXISTS " + this.getTableName());

        // Create tables again
        onCreate(db);
    }

    boolean add(String key, String value) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_KEY, key); // Contact Name
        values.put(KEY_VAL, value); // Contact Phone

        // Inserting Row
        long res = db.insertOrThrow(this.getTableName(), null, values);
        db.close(); // Closing database connection
        return res >= 0;
    }

    String get(String key) {
        SQLiteDatabase db = this.getReadableDatabase();

        Cursor cursor = db.query(this.getTableName(), new String[] { KEY_VAL }, KEY_KEY + "=?", new String[] { key }, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();

        if(cursor == null)
            return null;

        String ret = null;
        try {
            ret = cursor.getString(0);
        } catch(CursorIndexOutOfBoundsException e) {
            ret = null;
        } catch(IllegalStateException e) {
            ret = null;
        }
        cursor.close();
        return ret;
    }

    int count(String key) {
        String countQuery = "SELECT  * FROM " + this.getTableName()+" WHERE "+KEY_KEY+" = ?";
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, new String[] { key });
        int ret = cursor.getCount();;
        cursor.close();

        // return count
        return ret;
    }

    void delete(String key) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(this.getTableName(), KEY_KEY + " = ?", new String[] { key });
        db.close();
    }

    int update(String key, String value)
    {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_KEY, key);
        values.put(KEY_VAL, value);

        // updating row
        return db.update(this.getTableName(), values, KEY_KEY + " = ?", new String[] { key });
    }
}