光标似乎没有在Android应用程序中工作

时间:2012-12-22 03:36:23

标签: java android cursor

我的光标似乎不起作用。有人能帮帮我吗? 实际上这里的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;
    }

2 个答案:

答案 0 :(得分:1)

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

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

作为您的查询。

答案 1 :(得分:0)

你可以尝试这个..

  if (cursor.moveToFirst())
            {                       
                for (int i = 0; i < cursor.getCount(); i++)
                {
                    //Your logic goes here//
                    cursor.moveToNext();
//                   
                }           
            }
            cursor.close();
            db.close();