sqlite Android - 如何获取特定col / row的值

时间:2012-03-08 16:08:22

标签: android sqlite

我有一个方法,我传递一个id,然后想要找到该表中与id匹配的行,并返回一个字符串,该行是该行的colLabel:

public String getIconLabel(int id){
        String label;
        String selectQuery = "SELECT "+colL" FROM " + allIcons + " WHERE " +colIconID + "="+id; 

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        label = //HELP

        return label;

    }

我不清楚如何将标签设置为所选行的特定列?

请帮助

2 个答案:

答案 0 :(得分:6)

if (null != cursor && cursor.moveToFirst()) {
    label = cursor.getString(cursor.getColumnIndex(COLUMN_ID));
}

答案 1 :(得分:4)

if(cursor != null)
{
cursor.moveToFirst();
String label = cursor.getString(0);
}

0参数表示列索引。请参阅此link