选择*返回空光标

时间:2012-08-18 19:42:11

标签: android sqlite cursor

我在表格中插入了一条记录。它成功,因为.insert()返回新的行ID,我使用该id来查询原因_id = id。 我成功回归

但是当我使用SELECT * FROM TABLE(无原因)查询时,它返回空光标!

请帮我解决这个问题

这是插入方法

public String insertCategory(String name, int color) {
    SQLiteDatabase db = getWritableDatabase();

    ContentValues cv = new ContentValues();
    cv.put(COLUMN_NAME, name);
    cv.put(COLUMN_COLOR, color);

    long id;
    try {
        id = db.insertOrThrow(TABLE_CATEGORIES, null, cv);
    } catch (Exception e) {
        return null;
    }

    Cursor row = getRowById(db, TABLE_CATEGORIES, id, new String[] {
            COLUMN_ID, COLUMN_NAME });
    if (row.getCount() > 0) {
        if (row.moveToNext()) {
            return row.getString(row.getColumnIndex(COLUMN_NAME));
        } else
            return null;
    } else
        return null;
}

这是我用来查询原因的方法

private Cursor getRowById(SQLiteDatabase db, String table, long id,
        String[] columns) {
    Cursor cursor = db.query(table, columns, COLUMN_ID + " = ?",
            new String[] { "" + id }, null, null, null);
    return cursor;
}

最后,这里是我用于查询所有行的方法

public Cursor getAllCategories() {
    SQLiteDatabase db = getWritableDatabase();
    Cursor cursor = db.query(TABLE_CATEGORIES, null, null, null, null,
            null, null);

    db.close();
    return cursor;
}

感谢您的帮助

0 个答案:

没有答案
相关问题