使用Cursor获取字段值

时间:2022-05-15 22:39:22

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:

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

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),但不获取值。有关如何实现这一目标的任何建议?

3 个解决方案

#1


85  

I think you can forget about checking for null.

我想你可以忘记检查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

#2


15  

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

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

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 getColumnIndex with hardcoded strings.

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

#3


7  

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();        
}

#1


85  

I think you can forget about checking for null.

我想你可以忘记检查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

#2


15  

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

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

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 getColumnIndex with hardcoded strings.

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

#3


7  

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();        
}