【问题标题】:Android sqlite select statement troubles [duplicate]Android sqlite select语句麻烦[重复]
【发布时间】:2013-05-18 04:51:23
【问题描述】:

我正在尝试从名为 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”不存在

【问题讨论】:

  • 您是否在某处遇到异常?究竟是什么问题?
  • 我忘记添加了,已编辑!
  • 它表示没有任何名称为"_id"的列检查您的数据库定义
  • 我有这个专栏:private static final String COURSE_ROWID = "_id";

标签: java android database sqlite


【解决方案1】:

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

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

return mDb.query("Course",values, null, null, null, null, null, null);

【讨论】:

    【解决方案2】:

    将 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 列)。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 1970-01-01
    相关资源
    最近更新 更多