2013-05-17 53 views
0

我想从名为课程,具有属性名称的SQLite表中获取一些数据。Android的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”不存在

+1

你是否在某处遇到异常?究竟是什么问题? –

+0

我忘了添加它,编辑! – wilxjcherokee

+0

它是说没有任何名称为“_id”的列检查您的数据库定义 –

回答

0

在你的数据库如果你想所有课程名称更改与"_id"

重命名"CourseID"像这样返回语句。

return mDb.query("Course",values, null, null, null, null, null, null); 
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列)。

+0

但我的COURSE_ROWID已被声明为“_id” – wilxjcherokee

+0

请参阅下面的链接,可能会帮助您http://stackoverflow.com/questions/3236203/illegalargumentexception-column-id-does-not-exist-when-call-to- simplecursora和http://stackoverflow.com/questions/6573814/simplecursoradapter-is-being-difficult-to-use – saravana