Question

I have the following database config

public static final String ST_ID = "s_id";  
public static final String ST_NAME = "s_name";
public static final String ST_COURSE = "s_course";
public static final String DBCREATE_STUDENTDB = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME+" ("                  
                + "s_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                + "s_name VARCHAR, "
                + "s_course VARCHAR);";
        public static final int VERSION = 1;

Now I am trying to get the individual id

final ContentResolver resolver = getContentResolver();
final String[] projection = { StudentDB.ST_NAME, StudentDB.ST_COURSE };
Cursor cursor = resolver.query(StudentDataProvider.studentTableUri, projection, null,  null, null);

if (cursor.moveToFirst()) {
    do {

        Log.wtf("ID", ""+cursor.getString(cursor.getColumnIndex(StudentDB.ST_ID)));

    } while (cursor.moveToNext());
}

I am getting error on this.

10-02 07:14:29.788: E/AndroidRuntime(2252): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

however the following code works for other column

Log.wtf("List", ""+cursor.getString(cursor.getColumnIndex(StudentDB.ST_COURSE)));

what is the problem?

Was it helpful?

Solution

You didn't add your ID in your Projection Change it to :

final String[] projection = { StudentDB.ST_NAME, StudentDB.ST_COURSE,StudentDB.ST_ID};

OTHER TIPS

final String[] projection = { StudentDB.ST_NAME, StudentDB.ST_COURSE };

If no ID in your projection, the cursor don't have any field called so.

Do this:

final String[] projection = { StudentDB.ST_ID, StudentDB.ST_NAME, StudentDB.ST_COURSE };

@Sardor Dushamov, was right too, if ID is autoincrement, use getInt not getString.

See your projection String[], you didn't add the StudentDB.ST_ID

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top