使用游标获取字段值 [英] Get the field value with a Cursor

查看:199
本文介绍了使用游标获取字段值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建应用程序,并且Cursor遇到问题.我有一个SQLiteDatabase,当我尝试使用此函数获取值时会返回Cursor:

I'm creating an application and I have problems with Cursor. I have an SQLiteDatabase that returns me a Cursor when I try to fetch the values with this function:

public Cursor fetchOption(long rowId) throws SQLException {

    Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
        KEY_TITLE, KEY_BODY}, KEY_ROWID + "=" + rowId, null,
        null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;

}

我不知道如何获取Cursor中字段的值.如果我这样做的话:

I don't know how to obtain the value of the field in the Cursor. If I do that like so:

String a = mOptionDb.fetchOption(0).getColumnName(0).toString();
String b = mOptionDb.fetchOption(0).getColumnName(1).toString();
String c = mOptionDb.fetchOption(0).getColumnName(2).toString();

我仅获取列的名称(_id, title, body),而不获取值.关于如何实现这一目标的任何建议?

I only obtain the name of the columns (_id, title, body) but not the values. Any suggestions on how to achieve this?

推荐答案

我认为您可以忘记检查null.

I think you can forget about checking for null.

代替检查是否有数据,然后使用光标访问列:

Instead check if there is data and then access the columns using the cursor:

Cursor cursor = fetchOption(0);

if (cursor.moveToFirst()) // data?
   System.out.println(cursor.getString(cursor.getColumnIndex("title")); 

cursor.close(); // that's important too, otherwise you're gonna leak cursors

阅读Android教程也很有意义.记事本教程似乎很合适: http://developer.android.com/guide/tutorials/notepad/index.html

It might also make sense to read an Android tutorial. The notepad tutorial seems to fit the bill: http://developer.android.com/guide/tutorials/notepad/index.html

这篇关于使用游标获取字段值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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