1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

时间:2024-02-15 14:33:30

相关推荐

Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist

出错原因:在查询整个sqlite数据库时,没有查询到 "_id" 这一列。

原来的代码是:mSQLiteDatabase.query(table_name, new String[] {_title}, null, null, null, null, null);

修改后的代码为:mSQLiteDatabase.query(table_name, null, null, null, null, null, null);

这里的 new String[] {MyEvent._title} 是一个查询条件,数组里的内容是要查询表的列名,这里是限定你要查询的列。参数值为 null 表示查询所有列(包含有 “_id” 这一列) 。如里这样改也是可以 的:mSQLiteDatabase.query(table_name, new String[] {_id, _title}, null, null, null, null, null); 目的就是要你查询的结果中含有“_id” 这一列。

另外,关于sqlite数据库中的 "_id" 这一列,在sqlite数据库中是必须有的,而且列名还必须是 “_id” 一般为主键。而用以下代码在一个ListView中显示时,也需要用到 “_id” 这一列。

public void updataAdapter() {

if (mCursor != null && !mCursor.isClosed())

mCursor.close();

mCursor = m_MyDataBaseAdapter.fetchAllData();

mCursor.moveToFirst();

if (mCursor != null && mCursor.getCount() > 0) {

ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, mCursor, new String[] { _title }, new int[] { android.R.id.text1 });

listView.setAdapter(adapter);

}

}

这里的 SimpleCursorAdapter 在显示时会根据"_id"进行显示,“_id” 是必须有的。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。