Android的sqlite的db.query导致CursorIndexOutOfBoundsException [英] Android sqlite db.query leads to CursorIndexOutOfBoundsException

查看:227
本文介绍了Android的sqlite的db.query导致CursorIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的数据库中选择某些行,但因为我一直得到指数越界我决定删除 WHERE 一部分,现在只是得到所有数据。
我已经使用了android教程 http://developer.android.com /training/basics/data-storage/databases.html#DbHelper 时,为了建立这种方法。但是,当我试图从我得到CursorIndexOutOfBoundsException查询得到的值。

I am trying to select certain rows from my database, but since I kept getting index out of bound I decided to remove the WHERE part for now and just get all of the data. I have used the android tutorial http://developer.android.com/training/basics/data-storage/databases.html#DbHelper , in order to create this method. But when I am trying to get values from the query I am getting CursorIndexOutOfBoundsException.

我的code:

String getSite() {
    SQLiteDatabase db = this.getReadableDatabase();
            //get all data for now
    Cursor c = db.query(
        TABLE_NAME,  // The table to query
        null,                               // The columns to return
        null,                                // The columns for the WHERE clause
        null,                            // The values for the WHERE clause
        null,                                     // don't group the rows
        null,                                     // don't filter by row groups
        null                                 // don't sort
        );
    c.moveToFirst();
        String test= c.getString(0); 
    c.close();
    return test;}
}

的错误:
     android.database.CursorIndexOutOfBoundsException:指数-1要求,尺寸为6

当我检查c.count()我有6,所以我不知道为什么,我指的是创建一个索引,可能越界

when I checked for c.count() I got 6, so I am not sure why I am referring to an index that might out of bound

感谢您

推荐答案

您的光标是空的,如果它不动了。

Your cursor is empty if it didn't move.

尝试

if (c.moveToFirst()) {
    String test= c.getString(0);
    // or String test= c.getString(c.getColumnIndex("column_name")); 
}

这篇关于Android的sqlite的db.query导致CursorIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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