cursor index out of bounds exception

时间:2012-03-16 14:13:26

标签: android database sqlite cursor

打开a后,我得到一个光标索引超出范围错误                 数据库。请任何人告诉我如何打开现有的数据库                 sqllite - Android。我想在数据库上触发一个select查询                 检索一些信息?

            public void getPatient(SQLiteDatabase db, String name) {
    // TODO Auto-generated method stub
    db=this.getReadableDatabase();
    //db.open();
    System.out.println("in cursooooooooorrrrrrrrr");
    Cursor c = db.rawQuery("SELECT * from table_patient WHERE COL_Name"+"=?", 
            new String []{name});           
    //c.moveToFirst();

    int index = c.getColumnIndex("COL_FirstName");
    System.out.println("First Name : -----------  "+c.getString(index));
    c.close();


}

2 个答案:

答案 0 :(得分:3)

这样做:

int index = 0;

if(c.moveToFirst())
    index = c.getColumnIndex("COL_FirstName");

答案 1 :(得分:0)

您必须取消注释c.moveToFirst()。

这是一个例子:

public WhoisEntry[] getAllEntries(){        
    SQLiteDatabase db = getReadableDatabase();
    String sql = "SELECT * FROM domains_events";
    Cursor result = db.rawQuery(sql, null);
    WhoisEntry[] resultArray = new WhoisEntry[result.getCount()];
    int i=0;
    result.moveToFirst();
    while (i< result.getCount()){
        long date = result.getLong(result.getColumnIndex("exp_date")) * 1000;
        String domainName = result.getString(result.getColumnIndex("domainname"));
        resultArray[i] = new WhoisEntry(domainName, new Date(date));
        //resultArray[i].domainName = 
        //resultArray[i].expirationDate = result.getString(result.getColumnIndex("exp_date"));
        String name = result.getString(result.getColumnIndex("domainname"));
        i++;
    }
    return resultArray;
}