0

我的光标似乎不起作用。有人能帮帮我吗?实际上这里的 for 循环不起作用。未显示日志。这是我的代码:

public String getAFact(int rowNumber)
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("select mfacts from mfacts where osl_number=" + rowNumber + ";", null);
        for(c.moveToFirst();!c.isAfterLast();c.moveToNext()) {
            rowData = c.getString(c.getColumnIndex(KEY_NAME));
            Log.i("log_tag", "cursor isn't f**ked up..."+rowData);
        }
        c.close();
        db.close();
        return rowData;
    }
}

但无论如何,以下代码工作正常,并且正确显示了记录数!

public int countRowsInDb()
    {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor c = db.rawQuery("select * from mfacts",null);
        Log.i("Number of Records"," :: "+c.getCount());
        db.close();
        int c_getCount = c.getCount(); 
        c.close();
        return c_getCount;
    }
4

2 回答 2

1

我认为您的 SQL 语句已关闭。看起来您正在尝试获取整行数据,请尝试

"select * from mfacts where osl_number=" + rowNumber + ";"

作为您的查询。

于 2012-12-22T03:57:22.993 回答
0

你可以试试这个..

  if (cursor.moveToFirst())
            {                       
                for (int i = 0; i < cursor.getCount(); i++)
                {
                    //Your logic goes here//
                    cursor.moveToNext();
//                   
                }           
            }
            cursor.close();
            db.close();
于 2012-12-22T05:56:06.300 回答