Java 使用 Cursor 获取字段值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/903343/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 20:52:30  来源:igfitidea点击:

Get the field value with a Cursor

javaandroidandroid-sqlite

提问by Micha?l

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

我正在创建一个应用程序,但我遇到了Cursor. 当我尝试使用此函数获取值时SQLiteDatabase,我有一个返回 aCursor的值:

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;

}

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

我不知道如何获取Cursor. 如果我这样做:

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

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

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

采纳答案by Mariano Kamp

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

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

阅读 Android 教程也可能有意义。记事本教程似乎符合要求:http: //developer.android.com/guide/tutorials/notepad/index.html

回答by Josef Pfleger

You can use the Cursor's get*methods to retrieve values from the result:

您可以使用Cursorget*方法从结果中检索值:

long id = cursor.getLong(cursor.getColumnIndex("_id"));
long title = cursor.getString(cursor.getColumnIndex("title"));
...

Better practice is obviously to use constants (often provided by ContentProviders) instead of calls to getColumnIndexwith hardcoded strings.

更好的做法显然是使用常量(通常由 ContentProviders 提供)而不是getColumnIndex使用硬编码字符串调用。

回答by Mariano Kamp

You can use this mechanism.

您可以使用此机制。

Cursor record=db.test(finalDate);     
if(record.getCount()!=0){
    if(record.moveToFirst()){
        do{               
            Imgid1=record.getString(record.getColumnIndex(Database.PHOTO));                
        }while(record.moveToNext());                          
    } 
    record.close();        
}