Android数据库SQlite异常处理笔记

背景

在数据库的处理上使用了ORM框架GreenDao,数据库中存储了大量的文本数据;

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
	at android.database.CursorWindow.nativeGetLong(Native Method)
	at android.database.CursorWindow.getLong(CursorWindow.java:524)
	at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:75)
	at org.greenrobot.greendao.AbstractDao.loadCurrent(AbstractDao.java:541)
	at org.greenrobot.greendao.AbstractDao.loadUnique(AbstractDao.java:172)
	at org.greenrobot.greendao.AbstractDao.loadUniqueAndCloseCursor(AbstractDao.java:159)
	at org.greenrobot.greendao.InternalQueryDaoAccess.loadUniqueAndCloseCursor(InternalQueryDaoAccess.java:41)
	at org.greenrobot.greendao.query.Query.unique(Query.java:130)
	at org.greenrobot.greendao.query.QueryBuilder.unique(QueryBuilder.java:479)

处理

经查,可能造成该异常的原因暂有两个:

  1. 操作数据库时列名写错
  2. 游标Cursor保存的数据超出了限制(1-2m)

分析 由于使用了数据库框架GreenDao,第一种情况排除;最终确定为数据库中存储单条记录的文本数据过大引起,约84w个字符,2.38m,符合第二种情况

参考链接 https://blog.csdn.net/Gino_Zhang/article/details/49365059

https://stackoverflow.com/questions/11162031/android-cursor-maximum-size

https://stackoverflow.com/questions/21432556/android-java-lang-illegalstateexception-couldnt-read-row-0-col-0-from-cursorw