amin
amin

Reputation: 289

what happens to my first insertion in sqlite?

Summary

Using android studio and sqlite database, I'm trying to insert some rows to a table. the problem is when I wanna read them, it ignores the first row.

Codes

first insert 3 rows to DATASETS_INFO table:

        SQLiteDatabase db = this.getWritableDatabase();

        String query = "INSERT INTO `DATASETS_INFO`(`dataset_ID`, `dataset_name`, `type`, `RAPI`) " +
                "VALUES (122, 'avg temp salon 2', 1, '3avwVmZpw00')";
        db.execSQL(query);

        query = "INSERT INTO `DATASETS_INFO`(`dataset_ID`, `dataset_name`, `type`, `RAPI`) " +
                "VALUES (125, 'avg hum salon 2', 2, '75uNfLLy300')";
        db.execSQL(query);

        query = "INSERT INTO `DATASETS_INFO`(`dataset_ID`, `dataset_name`, `type`, `RAPI`) " +
                "VALUES (126, 'max hum salon 2', 2, 'b8rwdUM4F00')";
        db.execSQL(query);

then reading them:

        SQLiteDatabase db = this.getWritableDatabase();
        String query = "Select dataset_name, type FROM DATASETS_INFO";
        Cursor myCursor = db.rawQuery(query, null);
        Log.d("DatabaseHelper", "myCursor.getCount(): " + myCursor.getCount());
        DatasetInfo[] datasetInfos = new DatasetInfo[myCursor.getCount()];
        if (myCursor.moveToFirst()) {
            int i=0;
            while (myCursor.moveToNext()) {
                Log.d("DatabaseHelper", "i: " + i + ", dataset_name: " + myCursor.getString(0));
                datasetInfos[i] = new DatasetInfo();
                datasetInfos[i].dataset_name = myCursor.getString(0);
                datasetInfos[i].type = myCursor.getInt(1);
                ++i;
            }
        }

output: as u can see it ignores the first row and starts from the second row:

07-24 10:38:06.317 7253-7253/com.example.black3rror.mpandroidchart D/DatabaseHelper: myCursor.getCount(): 3
07-24 10:38:06.317 7253-7253/com.example.black3rror.mpandroidchart D/DatabaseHelper: i: 0, dataset_name: avg hum salon 2
07-24 10:38:06.317 7253-7253/com.example.black3rror.mpandroidchart D/DatabaseHelper: i: 1, dataset_name: max hum salon 2

its interesting that getCount() returns 3 but I don't know where is the first one.

Upvotes: 0

Views: 36

Answers (2)

Jordan Lefébure
Jordan Lefébure

Reputation: 81

You should use do while loop

          do {
            Log.d("DatabaseHelper", "i: " + i + ", dataset_name: " + myCursor.getString(0));
            datasetInfos[i] = new DatasetInfo();
            datasetInfos[i].dataset_name = myCursor.getString(0);
            datasetInfos[i].type = myCursor.getInt(1);
            ++i;
        } while (myCursor.moveToNext());

Upvotes: 0

Kaushal28
Kaushal28

Reputation: 5565

The reason why 1st record is skipped is that you are calling moveToFirst() and then without reading it (which is the first record), you are calling moveToNext(). So the first record is skipped between these calls.

moveToFirst() is used when you need to start iterating from the start after you have already reached some position. In your case, you are already at the first position. Following should work:

int i = 0;
  do  {
   Log.d("DatabaseHelper", "i: " + i + ", dataset_name: " + myCursor.getString(0));
        datasetInfos[i] = new DatasetInfo();
        datasetInfos[i].dataset_name = myCursor.getString(0);
        datasetInfos[i].type = myCursor.getInt(1);
        ++i;
} while (myCursor.moveToNext());

Upvotes: 1

Related Questions