cursor index out of bounds exception

2019-05-25 01:13发布

I am getting a Cursor Index out of bounds error after opening a database . Please can any one tell me how to open an existing database in sqllite - Android . I want to fire a select query on the database and retrieve some information ?

            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条回答
做个烂人
2楼-- · 2019-05-25 01:34

You must uncomment c.moveToFirst().

Here an example:

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;
}
查看更多
甜甜的少女心
3楼-- · 2019-05-25 01:46

do it like this:

int index = 0;

if(c.moveToFirst())
    index = c.getColumnIndex("COL_FirstName");
查看更多
登录 后发表回答