开发者

getting row id of a database

what i want to do is do a search of my database for a string then find out what the row id is where that string is.

I thought by doing this

    public void getRow(){

    ContactDB db = new ContactDB(this);
    db.open();
    Cursor c = db.getId("1234567890");
    String test = c.getString(c.getColumnIndex(db.PHONE_NUMBER));
    Log.v("Contact", "Row ID: " + test);
    db.close();

database class

public Cursor getId(String where){
    return db.query(DATABASE_TABLE, new String[] {ID},where,null,null,null,null);
}

that it would give me what i want but i get a "cursor index out of bounds" error, how should i be doing 开发者_运维百科this?


change getId to:

     public Cursor getId(String where){
        Cursor c = db.query(DATABASE_TABLE, new String[] {ID},where,null,null,null,null);
        if (c != null) c.moveToFirst();
        return c;
     }


You need to do c.moveToFirst() before tying to read any information.

Also do c.close() when you're done.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜