Android开发——CursorIndexOutOfBoundsException异常

来源:互联网 发布:天谕流光男性捏脸数据 编辑:程序博客网 时间:2024/06/06 12:38

  android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1。

  这是今天博主做一个update操作时遇到的问题,在网上搜索一波发现android中数据库处理使用cursor时,游标不是放在为0的下标,而是放在为-1的下标处开始的。但是并不是意味着可以直接取-1,也就是说返回给cursor查询结果时,不能够马上从cursor中提取值。

  那么该怎么取呢?先调用moveToNext()或moveToFirst()就可以了。具体用法如下:

  

Cursor cursor = db.query("tbl_book",new String[]{"_id"}, "name=?",new String[]{name}, null, null, null);int id = 0;if(cursor.moveToFirst()){id= cursor.getInt(0);  }cursor.close();




0 0
原创粉丝点击