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
final String[] projection = { StudentDB.ST_NAME, StudentDB.ST_COURSE };

如果您的投影中没有 ID,则光标没有任何名为 so 的字段。

做这个:

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

@Sardor Dushamov,也是对的,如果 ID 是自动增量的,请使用 getInt 而不是 getString。

于 2013-10-02T07:27:16.130 回答
2

您没有在 Projection 中添加您的 ID 将其更改为:

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

查看您的投影字符串 [],您没有添加StudentDB.ST_ID

于 2013-10-02T07:29:52.353 回答