0

次のデータベース構成があります

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;

今、私は個々の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());
}

これでエラーが発生します。

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.

ただし、次のコードは他の列に対して機能します

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

何が問題ですか?

4

3 に答える 3

2

Projection に ID を追加しませんでした。次のように変更します:

final String[] projection = { StudentDB.ST_NAME, StudentDB.ST_COURSE,StudentDB.ST_ID};
于 2013-10-02T07:27:45.887 に答える
0

射影 String[] を参照してください。StudentDB.ST_ID

于 2013-10-02T07:29:52.353 に答える