rawQuery return always return false

3.2k Views Asked by At

I am trying to iterate on my database records. but for some reason it always return an empty cursor. If I pull out from the emulator the database file file the 'File explorer tab' I can see the table and the data in it.

here is my code:

Cursor result = db.getReadableDatabase().rawQuery(
                    "SELECT _ID, STUDENT_NAME, STUDENT_GRADE, STUDENT_ADDRESS "
                            + "FROM students", null);
            while (!result.moveToNext())
            {
                int id = result.getInt(0);
                String studentName = result.getString(1);
                String studentGrade = result.getString(2);
                String studentAddress = result.getString(3);
                Log.d("Sqlitedemo", "ID=" + id + ", STUDENT_NAME=" + studentName +", STUDENT_GRADE="+studentGrade+", STUDENT_ADDRESS="+studentAddress);
            }
            result.close();

now while (!result.moveToNext()) always return false.

this is my DatabaseHelper class:

public class DatabaseHelper extends SQLiteOpenHelper
{
    private static final String DATABASE_NAME   = "db";
    static final String         STUDENT_NAME    = " STUDENT_NAME";
    static final String         STUDENT_GRADE   = "STUDENT_GRADE";
    static final String         STUDENT_ADDRESS = "STUDENT_ADDRESS";

    public DatabaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db)
    {
        // creating the table
        db
                .execSQL("CREATE TABLE students (_id INTEGER PRIMARY KEY AUTOINCREMENT, STUDENT_NAME TEXT, STUDENT_GRADE TEXT,  STUDENT_ADDRESS TEXT);");

        ContentValues cv = new ContentValues();

        cv.put(STUDENT_NAME, "Roi");
        cv.put(STUDENT_GRADE, "85");
        cv.put(STUDENT_ADDRESS, "Tel aviv");

        db.insert("students", STUDENT_NAME, cv);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {
        // TODO Auto-generated method stub

    }

}

any idea?

Thanks, ray.

2

There are 2 best solutions below

2
On BEST ANSWER
Cursor result = db.getReadableDatabase().rawQuery(
                "SELECT _ID, STUDENT_NAME, STUDENT_GRADE, STUDENT_ADDRESS "
                        + "FROM students", null);
int id = result.getInt(0);
String studentName = result.getString(1);
String studentGrade = result.getString(2);
String studentAddress = result.getString(3);
if (result.moveToFirst())
{
    Log.d("Sqlitedemo", "ID=" + id + ", STUDENT_NAME=" + studentName +", STUDENT_GRADE="+studentGrade+", STUDENT_ADDRESS="+studentAddress);
    while(result.moveToNext())
    {
        Log.d("Sqlitedemo", "ID=" + id + ", STUDENT_NAME=" + studentName +", STUDENT_GRADE="+studentGrade+", STUDENT_ADDRESS="+studentAddress);
    }
}
result.close();
0
On
  while (!result.moveToNext())

Remove the ! and it will magically start working. Your current code is saying "while there are NOT more results..."