0

我正在尝试从名为 Course 的 sqlite 表中获取一些数据,其中包含属性名称。

我在这里建表。

private static final String COURSE_ID = "CourseID";
private static final String COURSE_NAME = "Name";
private static final String COURSE_CODE = "CourseCode";
private static final String COURSE_ROWID = "_id";
private static final String COURSE_CREATE =
        "create table " +
"Course" + " ( " + 
COURSE_ROWID + " integer primary key autoincrement, " +
COURSE_ID + " integer not null," 
+ COURSE_NAME + " text not null, " +
COURSE_CODE + " text not null" + ");";

我尝试使用此功能选择我的数据。

    public Cursor getCourseNames() throws SQLException {
    String[] values = {COURSE_NAME};
    mDb = mDbHelper.getReadableDatabase();
    return mDb.query("Course",values, COURSE_ROWID + "=" + "Name", null, null, null, null, null); 

}

然后在我的主课中,我像这样执行它。

   public void buildCoursetoChapterList(){

Cursor cursor = dbHelper.getCourseNames();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this, android.R.layout.simple_list_item_1, cursor, null, null);

ListView listView = (ListView) findViewById(R.id.list);

listView.setAdapter(adapter);


}

我只想获取数据并放入列表视图中,知道我做错了什么吗?这似乎是合乎逻辑的Select from Course WHERE _id = "Name"

哦嘟嘟我忘记了我的错误... java.lang.IllegalArgumentException:列'_id'不存在

4

2 回答 2

0

在您的数据库中重命名"CourseID""_id"

如果您希望所有课程名称都像这样更改返回语句。

return mDb.query("Course",values, null, null, null, null, null, null);
于 2013-05-17T19:29:34.933 回答
0

将 COURSE_ROWID 设为“_id”

"create table " +
"Course" + " ( " + 
COURSE_ROWID + " AS _id," +
COURSE_ID + " integer not null," 
+ COURSE_NAME + " text not null, " +
COURSE_CODE + " text not null" + ");";

这适用于尚未指定为“整数主键自动增量”的行 ID(所有表都有一个行 ID 列)。

于 2013-05-17T20:00:08.863 回答