在 Android 4.2 API 17 上读取 Sqlite Cursor carsh [英] Reading Sqlite Cursor carsh on Android 4.2 API 17

查看:24
本文介绍了在 Android 4.2 API 17 上读取 Sqlite Cursor carsh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 145 行的表格.当我尝试获取所有数据时,它在 android 4.2崩溃但是它在 android 4.4 模拟器上运行良好.

I have a table with 145 rows. when i try to get all of it's data it getting crashed on android 4.2, BUT it works fine on android 4.4 emulator.

public ArrayList<QuestionInfoHolder> getAllArticle()
    {
        ArrayList<QuestionInfoHolder> result = new ArrayList<QuestionInfoHolder>();
        SQLiteDatabase db = getReadableDatabase();
        String query = "SELECT * FROM " + ARTICLE_TABLE_NAME + " ORDER BY "+ QUESTION_DATE +";";
        Cursor cursor = db.rawQuery(query, null);
        if(cursor != null)
        {
            for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext())
            {
                result.add(new QuestionInfoHolder(cursor.getInt(0),
                        cursor.getInt(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        cursor.getString(4)));
            }
        }

        return result;
    }

日志猫:

Failed to read row 9, column 0 from a CursorWindow which has 9 rows, 5 columns.
FATAL EXCEPTION: main
java.lang.IllegalStateException: Couldn't read row 9, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

这表示游标只有 9 行,但如果我记录 cursor.getCount(); 它将显示 145.

this says the cursor has only 9 rows, but if i log cursor.getCount(); it will show 145.

我不知道是什么问题.所以任何帮助或建议将不胜感激.

i can not figure out what is the problem. so any help or suggestion will be appreciated.

更新 1:

DatabaseUtils.dumpCursor(cursor); 日志:

I/System.out: 99 {
E/CursorWindow: Failed to read row 9, column 0 from a CursorWindow which has 9 rows, 5 columns.
D/AndroidRuntime: Shutting down VM
W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x41b5e9a8)
E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Couldn't read row 9, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:434)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at android.database.DatabaseUtils.dumpCurrentRow(DatabaseUtils.java:562)
at android.database.DatabaseUtils.dumpCursor(DatabaseUtils.java:499)
at android.database.DatabaseUtils.dumpCursor(DatabaseUtils.java:482)

推荐答案

底层 Android CursorWindow 被限制为 2MB 的数据.尝试使用具有更多数据的 Cursor 将导致这些无法读取"异常.

Underlying Android CursorWindow is limited to 2MB of data. Attempting to work with a Cursor that has more data than that will cause these "failed to read" exceptions.

您可以通过在查询中不包含包含大量数据的列来解决此问题.将 SELECT * 替换为没有任何大数据列的更具选择性的 SELECT column1,column2,....

You can work around it by not including columns with large data in your query. Replace the SELECT * with a more selective SELECT column1,column2,... without any large data columns.

总的来说,您不应在 Android sqlite 数据库中存储任何大数据,例如图像或文件内容.将大数据存储在文件系统中,并将路径存储在数据库中.

Overall you should not store any large data such as images or file content in an Android sqlite database. Store the large data in the filesystem and just store the path in your database.

这篇关于在 Android 4.2 API 17 上读取 Sqlite Cursor carsh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆