android.database.CursorIndexOutOfBoundsException:请求索引-1,大小为1

Eug*_*ene 3 sqlite android

我总是android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1在下面的代码中得到例外.请指出什么是错的.

public boolean Foo(String str) {

        Cursor c = dbHelper.getItemByTitle(str);
        if (c.getCount() == 0) {
            return false;
        } else {
            c.getString(1) // Irrespective of what argument put here I get this exception
            return true;
        }
    }
Run Code Online (Sandbox Code Playgroud)

Jon*_*eet 6

我没有使用Sqlite,但我怀疑你需要moveToNext()在询问值之前调用第一行:

if (!c.moveToNext()) {
    return false;
}
String value = c.getString(1);
...
Run Code Online (Sandbox Code Playgroud)

请注意,getCount()返回结果集中的数,但参数getString()指示号.另请注意,就我从文档中看到的而言,这里的列号从零开始,与基于一的JDBC不同.