Android:“_id"列不存在 [英] Android: column '_id' does not exist

查看:45
本文介绍了Android:“_id"列不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到此错误

IllegalArgumentException: 列_id"不存在

IllegalArgumentException: column '_id' does not exist

当使用 SimpleCursorAdapter 从我的数据库中检索时,该表确实有这个 _id 列.注意到这是一个常见问题,我尝试解决它,给出了一些在线解决方案,但没有一个有效.这是我的光标查询:

When using a SimpleCursorAdapter to retrieve from my database, and the table does indeed have this _id column. Noticing this a common problem, I have tried to work around it given some of the solutions online but none of them work. This is my cursor query:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.quoterow, myCursor, new String[]{"_id", "quote"}, new int[]{R.id.quote});

虽然我应该提到原始没有包含 _id 列,但我最近添加了这个以尝试解决问题.有没有人有任何可能有助于解决问题的想法?

although I should mention the original did not include the _id column, I added this recently to try and solve the problem. Has anyone got any ideas that might help solve the problem?

推荐答案

您的数据库不必有名为_id"的列,但 SimpleCursorAdaptor 确实需要返回一个列.您可以使用别名来执行此操作.

Your database doesn't have to have a column called '_id' but the SimpleCursorAdaptor does need to have one returned. You can do this with an alias.

一个例子是我有一个带有列的表...

An example is that I have a table with columns...

uid,name,number

为了查询 SimpleCursorAdapter,我使用数据库 rawQuery...

To query this for a SimpleCursorAdapter, I do this with a database rawQuery...

SELECT uid as _id,name,number FROM MY_TABLE

这工作正常,并为 SimpleCursorAdapter 提供必要的_id"列.

This works fine and supplies the necessary '_id' column to SimpleCursorAdapter.

据我所知,_id 字段用作唯一键,以确保光标处理的数据可以由适配器和适配器视图等正确处理.

As far as I understand it the _id field is used as a unique key to make sure the data the cursor handles can be handled correctly by adapters and adapterviews etc.

查看内容提供者.

在任何类型的数据库"中使用唯一键是非常普遍的做法,据我所知,使用列名_id"(或_ID")只是一种标准化和跨数据库、内容提供者、游标、适配器等简化事情

Using a unique key in 'databases' of whatever kind is pretty much universal practice, and as far as I can tell, the use of the column name '_id' (or '_ID') is simply a way of standardizing and simplifying things across databases, content providers, cursors, adapters etc etc

简而言之,为了使这些不同的组件正常工作,他们需要一个具有唯一值的数据列,但他们还必须知道"该列的名称.可以这么说,他们不会知道"我的列名uid"是他们需要的,而不是我的姓名"和编号"列.

In short, in order for these various components to work correctly, they need a data column with unique values but they must also 'know' what the name of that column is. They wouldn't 'know', so to speak, that my column name 'uid' is the one they need as opposed to my 'name' and 'number' columns.

这篇关于Android:“_id"列不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆