android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
作者:fly 发布于:2014-2-11 15:56 分类:android
android 中数据库处理,特别是使用cursor时,注意初始位置,好像是从下标为-1的地方开始的,也就是说一次查询中,返回给cursor查询结果时,不能够马上从cursor中提取值。
比如,下面的代码会返回错误,android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0:
int score = ((Cursor)getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1")).getInt(0); 正确的用法: Cursor cursor = getReadableDatabase().query(TABLE_NAME, new String[]{"learned"}, "_id=?", new String[]{""+id}, null, null, null,"1"); int learned=0; if(cursor.moveToFirst()){ score= cursor.getInt(0); } cursor.close();
日历
分类
存档
- 2018年3月(1)
- 2017年9月(1)
- 2017年7月(2)
- 2017年5月(1)
- 2017年3月(2)
- 2017年2月(6)
- 2017年1月(9)
- 2016年12月(13)
- 2016年11月(4)
- 2016年8月(17)
- 2016年7月(4)
- 2016年3月(1)
- 2016年2月(2)
- 2015年12月(4)
- 2015年10月(5)
- 2015年9月(1)
- 2015年8月(1)
- 2015年6月(2)
- 2015年5月(4)
- 2015年4月(1)
- 2015年3月(4)
- 2015年2月(2)
- 2015年1月(24)
- 2014年12月(3)
- 2014年10月(5)
- 2014年9月(5)
- 2014年8月(4)
- 2014年7月(13)
- 2014年6月(18)
- 2014年5月(20)
- 2014年4月(23)
- 2014年3月(90)
- 2014年2月(121)
- 2014年1月(11)
最新评论
- qq
哈 - AdOnCn
很好!感谢! - FlyLu
fly您好,看到你写的文章很不错,也很佩... - 地铁诡事
[给力] - 婚嫁百科
如果有爱,如果有结局! - 安安博客
看不懂 - wys.me
博主好,请教个问题哈。 怎么修改vis... - 知道91博客
哈哈,这个算是编程很基础很基础的了吧 - 婚嫁百科
晕死,怎么都不会显示出来的啊 - 小俊
看不懂
发表评论: