Akshat Chaturvedi
Akshat Chaturvedi

Reputation: 1

What is the error in this cursor created for database?

I am creating a SQLite database in android. But there is some error appearing up whenever I call the method "displayDatabase()".

Please Help!!

Here is the displayDatabase() Method:

public void displayDatabase() {

    DataDbHelper mDbHelper = new DataDbHelper(this);

    SQLiteDatabase db = mDbHelper.getReadableDatabase();

    String[] projection = {DataContract.DataEntry.COLUMN_PROJECT_NAME,
            DataContract.DataEntry.COLUMN_HEAD,
            DataContract.DataEntry.COLUMN_CITY,
            DataContract.DataEntry.COLUMN_COST};
    Cursor c = db.query(DataContract.DataEntry.TABLE_NAME,
            projection,
            null,
            null,
            null,
            null,
            null);
    TextView textView = (TextView) findViewById(R.id.textview);

    try {
        //textView.setText("The database contains - " + c.getColumnCount() + "Columns containing data");

        textView.setText("Hello Welcome\n");

        textView.append("-" + DataContract.DataEntry._ID
                + "---" + DataContract.DataEntry.COLUMN_PROJECT_NAME
                + "---" + DataContract.DataEntry.COLUMN_HEAD
                + "---" + DataContract.DataEntry.COLUMN_CITY
                + "---" + DataContract.DataEntry.COLUMN_COST + "\n");


        int currentId = c.getColumnIndex(DataContract.DataEntry._ID);
        int projectNameId = c.getColumnIndex(DataContract.DataEntry.COLUMN_PROJECT_NAME);
        int headId = c.getColumnIndex(DataContract.DataEntry.COLUMN_HEAD);
        int cityId = c.getColumnIndex(DataContract.DataEntry.COLUMN_CITY);
        int costId = c.getColumnIndex(DataContract.DataEntry.COLUMN_COST);


        while (c.moveToNext()) {
            int id = c.getInt(currentId);
            String projectName = c.getString(projectNameId);
            String head = c.getString(headId);
            String city = c.getString(cityId);
            String cost = c.getString(costId);

            textView.append("-" + id
                    + "---" + projectName
                    + "---" + head
                    + "---" + city
                    + "---" + cost);


        }


    } finally {
        c.close();
    }
}

And here is the error appearing in the logcat :

CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 11 rows, 4 columns. 03-15 10:00:58.359 6348-6348/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.student.sampledatabase, PID: 6348 java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Upvotes: 0

Views: 176

Answers (2)

MikeT
MikeT

Reputation: 57083

The Cause of the Error

The reason for the failure is that an attempt is being to get a column at offset -1, which will never exist as offsets can be from 0 to the number of columns in the cursor less 1.

The reason why -1 is being used is because that is the value returned from the Cursor getColumnIndex method when the column name passed to the method does not exist in the cursor.

The reason why you are getting the -1 is that you have not included the column who's name is as per DataContract.DataEntry._ID resolved in the cursor so the line :-

int currentId = c.getColumnIndex(DataContract.DataEntry._ID);

results in currentId being -1

Thus the above error when the following line is executed :-

int id = c.getInt(currentId);

The Fix

One fix, would be to specify null instead of projection, this would result in all columns of the table being retrieved and is the equivalent of using SELECT * FROM .......

e.g. by using :-

Cursor c = db.query(DataContract.DataEntry.TABLE_NAME,
        null,
        null,
        null,
        null,
        null,
        null);

Another fix would be to change :-

String[] projection = {DataContract.DataEntry.COLUMN_PROJECT_NAME,
        DataContract.DataEntry.COLUMN_HEAD,
        DataContract.DataEntry.COLUMN_CITY,
        DataContract.DataEntry.COLUMN_COST};

to instead be :-

String[] projection = {DataContract.DataEntry._ID,
        DataContract.DataEntry.COLUMN_PROJECT_NAME,
        DataContract.DataEntry.COLUMN_HEAD,
        DataContract.DataEntry.COLUMN_CITY,
        DataContract.DataEntry.COLUMN_COST};

Thus the column will then be included in the Cursor and the offset will be the offset of that column (0 in the case above as it is the first column in the result).

Upvotes: 0

PPartisan
PPartisan

Reputation: 8231

Add this column, DataContract.DataEntry._ID, to your projection.

Upvotes: 2

Related Questions