光标索引出界异常的 [英] Cursor Index Out of Bounds Exception

查看:143
本文介绍了光标索引出界异常的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图对我的数据库执行查询,我得到这个例外。但是,文档指出该方法 SQLiteDatabase.query(...)的回报,一个Cursor对象,它是定位在第一项之前,这是我除preT意味着光标在返回的行的开始。如果我添加了 Cursor.moveToFirst()光标访问数据之前,我没有得到任何的异常。到底是怎么回事?我是否需要试图让数据之前,总是称之为moveToFirst?该文件说,这种方法,将光标移动到第一行。

 光标C = db.query(TABLENAME,NULL,NULL,NULL,NULL,NULL,NULL);
Log.d(TAG,+ c.getInt(c.getColumnIndex(_ ID)));
 

解决方案

查询后,您需要调用下一个() moveToFirst()。光标懒加载,调用光标被加载到内存这些方法之后。您可以决定什么时候做。

In attempting to execute a query on my database, I get this exception. However, the documentation states that the method SQLiteDatabase.query(...) returns, "A Cursor object, which is positioned before the first entry," which I interpret to mean that the Cursor is at the start of the rows returned. If I add the Cursor.moveToFirst() before accessing data in the Cursor, I get no exception. What is going on? Do I need to always call "moveToFirst" before trying to get data? The documentation says this method, "moves the cursor to the first row."

Cursor c = db.query(TABLENAME, null, null, null, null, null, null);
Log.d("TAG",""+c.getInt(c.getColumnIndex("_id")));

解决方案

After query you need to call next() or moveToFirst(). Cursors are lazy loaded, after calling these methods cursor is loaded into memory. You can decide when to do it.

这篇关于光标索引出界异常的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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