关于使用CursorAdapter()时出现“column '_id' does not exist”错误的说明及解决方案
2011-02-21 17:21
886 查看
出现问题:
我们使用
SimpleCursorAdapter
()显示
SQLite
数据库表中的数据的时候,
Eclipse
会莫名其妙报错,报错如下:
显示:
java.lang.IllegalArgumentException: column '_id' does not exist
而假如我们的数据表列名中并未定义名为“
_id
”的列名,如:我的数据表中定义了三列,列名分别为:“
_id1
”
,
”
name1
”
,
”
bir
”,这样,表中就没有名为“
_id
”的数据库,(
ps
:如果你的数据表中刚好就把某一列定义成了
_id
,那么肯定是不会出现这种错误了)
[9]
查找原因:
仔细查看
Android
的开发文档中有关
SimpleCursorAdapter()
的说明发现,
SimpleCursorAdapter()
的父类是
CursorAdapter()
,
CursorAdapter()
有一个很隐蔽的规定
The Cursor must include a
column named "_id" or this class will not work.
也就是说,我们查询结果集游标
Cursor
返回的数据中,一定要有一列名为“
_id
”,否则这个类将不起作用,这也就是为什么
Eclipse
会报上面的错误。
解决方案:
这种情况无需修改数据库。为满足
CursorAdapter()
类的要求,我们可以在查询语句相关代码中作如下修改:(
ps
:
chi
数据表三列名为:
_id1,name1,bir
)
原代码:
public
Cursor getAll(){
return
db.rawQuery("SELECT * FROM chi", null);
}
修改后代码:
public
Cursor getAll(){
return
db.rawQuery("SELECT _id1 AS _id ,name1 , bir FROM chi", null);
//return
db.rawQuery("SELECT * FROM chi", null);
}
我们使用
SimpleCursorAdapter
()显示
SQLite
数据库表中的数据的时候,
Eclipse
会莫名其妙报错,报错如下:
显示:
java.lang.IllegalArgumentException: column '_id' does not exist
而假如我们的数据表列名中并未定义名为“
_id
”的列名,如:我的数据表中定义了三列,列名分别为:“
_id1
”
,
”
name1
”
,
”
bir
”,这样,表中就没有名为“
_id
”的数据库,(
ps
:如果你的数据表中刚好就把某一列定义成了
_id
,那么肯定是不会出现这种错误了)
[9]
查找原因:
仔细查看
Android
的开发文档中有关
SimpleCursorAdapter()
的说明发现,
SimpleCursorAdapter()
的父类是
CursorAdapter()
,
CursorAdapter()
有一个很隐蔽的规定
The Cursor must include a
column named "_id" or this class will not work.
也就是说,我们查询结果集游标
Cursor
返回的数据中,一定要有一列名为“
_id
”,否则这个类将不起作用,这也就是为什么
Eclipse
会报上面的错误。
解决方案:
这种情况无需修改数据库。为满足
CursorAdapter()
类的要求,我们可以在查询语句相关代码中作如下修改:(
ps
:
chi
数据表三列名为:
_id1,name1,bir
)
原代码:
public
Cursor getAll(){
return
db.rawQuery("SELECT * FROM chi", null);
}
修改后代码:
public
Cursor getAll(){
return
db.rawQuery("SELECT _id1 AS _id ,name1 , bir FROM chi", null);
//return
db.rawQuery("SELECT * FROM chi", null);
}
相关文章推荐
- simplecursoradapter使用时提示column '_id' does not exist
- java.lang.IllegalArgumentException: column '_id' does not exist
- Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
- Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
- java.lang.IllegalArgumentException: column '_id' does not exist 的相关
- Android Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
- java.lang.IllegalArgumentException: column '_id' does not exist
- java.lang.IllegalArgumentException: column '_id' does not exist
- java.lang.IllegalArgumentException: column '_id' does not exist
- 一夜的错误 : simpleCursorAdapter column '_id' does not exist
- java.lang.IllegalArgumentException: column '_id' does not exist
- 使用qtcreator时出现The specified source space "/home/xxx/src" does not exist的错误
- column '_id' does not exist
- JSP页面使用EL表达式出现的问题:javax.el.PropertyNotFoundException: Property 'ID' not found on type java.lang.Str
- MYSQL操作中错误The user specified as a definer ('root'@'%') does not exist的解决
- 错误:A SPI class of type org.apache.lucene.*** with name 'Whitespace' does not exist.
- 关于"使用注册为 allowDefinition='MachineToApplication' 的节是错误的"的解决方案
- 错误代码: 1449 The user specified as a definer ('root'@'%') does not exist
- 关于error C2871: ''std'' : does not exist or is not a namespace处理
- 关于使用MyEclipse启动web项目时工具报 The specified JRE installation does not exist 错误提示解决方法