安卓:列'_id'不存在 [英] Android: column '_id' does not exist

查看:152
本文介绍了安卓:列'_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

这工作正常和必要的_id一栏提供给SimpleCursorAdapter。

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

编辑:据我了解它的_id字段作为唯一密钥,以确保光标手柄可以通过正确的适配器来处理和数据adapterviews等

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.

看在文档的内容提供商的数据模型。

Look at the data model in the docs for Content Providers.

使用'数据库'的唯一密钥什么样的是pretty的很多普遍的做法,而据我所知,使用列名_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.

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

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