无法获取与sqlite

时间:2016-04-14 05:20:20

标签: android sqlite

我有以下代码,我试图抓住所有匹配id的行。

public ChartData getCompliedChartCounts(int id) {
        SQLiteDatabase db = this.getReadableDatabase();

        SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();


        Cursor cursor = db.query(TABLE_COMPLIANCE_CHART_COUNTS, new String[]{KEY_ID,
                        KEY_FILTER_ID, KEY_YEAR, KEY_COUNTRY_ID, KEY_DOMAIN_ID, KEY_COMPLIED_COUNT, KEY_NOT_COMPLIED_COUNT, KEY_DELAYED_COUNT, KEY_INPROGRESS_COUNT}, KEY_COUNTRY_ID + "=?",
                new String[]{String.valueOf(id)}, null, null, null, null);
        if (cursor != null)
            cursor.moveToFirst();
        ChartData country = new ChartData(cursor.getInt(0), cursor.getInt(1), cursor.getInt(2), cursor.getInt(3), cursor.getInt(4), cursor.getInt(5), cursor.getInt(6), cursor.getInt(7), cursor.getInt(8));
        // return contact
        return country;
    }

当我像getCompliedChartCounts(3)一样调用它时,我希望它抓取包含id 3的所有行。

但问题是,它只抓取与id 3匹配的第一行,而其余行未被抓取。我该如何解决这个问题?

1 个答案:

答案 0 :(得分:5)

亲爱的,你不会迭代光标。你只能获取你应该尝试的第一行

try {
    while (cursor.moveToNext()) {
        ...
    }
} finally {
    cursor.close();
}

并保留所有内容并抓取所有内容