Android:列“_id”不存在

发布于 2024-11-04 09:57:59 字数 464 浏览 6 评论 0原文

我收到这个错误

IllegalArgumentException:列“_id”不存在

当使用 SimpleCursorAdapter 从我的数据库检索时,表确实具有此 _id 列。注意到这是一个常见问题,我尝试根据网上的一些解决方案来解决它,但它们都不起作用。这是我的光标查询:

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

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

I am getting this error

IllegalArgumentException: column '_id' does not exist

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});

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生再见 2024-11-11 09:57:59

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

一个例子是,我有一个包含列的表...

uid,name,number

要查询 SimpleCursorAdapter,我使用数据库 rawQuery 执行此操作...

SELECT uid as _id,name,number FROM MY_TABLE

这工作正常,并向 SimpleCursorAdapter 提供必要的“_id”列。

编辑:据我了解, _id 字段用作唯一键,以确保适配器和适配器视图等可以正确处理光标处理的数据。

查看文档中的数据模型对于内容提供商

在任何类型的“数据库”中使用唯一键几乎是普遍的做法,据我所知,使用列名“_id”(或“_ID”)只是一种标准化和简化事物的方法数据库、内容提供程序、游标、适配器等

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

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

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

SELECT uid as _id,name,number FROM MY_TABLE

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

EDIT: 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.

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

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.

滥情哥ㄟ 2024-11-11 09:57:59

您的表中没有列“_id”,或者您没有将其包含在查询中。这就是导致异常的原因。您还需要修复以下问题:

CursorAdapter 构造函数的最后一个参数缺少 _id 的 to 列引用。

int[] 参数是一个视图 ID 数组,用于填充光标中的值。 String[] 参数是光标指向的行中的列名称数组。

from 数组中的值必须与 to 数组中的值数量相同。因为来自游标的数据是从游标中抓取并放置到视图中的。如果每个数组中的值数量不同,适配器将引发异常,因为它没有足够的信息来将数据映射到视图。

另外,根据 SimpleCursorAdapter 的 JavaDoc,该构造函数已被弃用,因为它会导致在 UI 线程中执行查询。这很糟糕。请改用这个:

http: //developer.android.com/reference/android/widget/SimpleCursorAdapter.html#SimpleCursorAdapter%28android.content.Context,%20int,%20android.database.Cursor,%20java.lang.String[ ],%20int[],%20int%29

一个简单的修复方法是将“,0”添加到参数列表的末尾。

You either don't have a column "_id" in your table or you are not including it in your query. That is what is causing the exception. You need to fix the following as well:

Your last argument for the CursorAdapter constructor is missing the to column reference for _id.

The int[] argument is an array of view ids to populate with values from the cursor. The String[] argument is an array of column names from a row the cursor points to.

You have to have an equal number of values in the from array as you do the to array. Because the data from the Cursor is being grabbed FROM the Cursor and placed TO the views. If there are not an equal number of values in each array, the adapter throws an exception because it doesn't have the right amount of information to map the data to the views.

Also, according to the JavaDoc for SimpleCursorAdapter, that constructor is deprecated because it causes queries to be executed in the UI thread. Which is bad. Use this one instead:

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#SimpleCursorAdapter%28android.content.Context,%20int,%20android.database.Cursor,%20java.lang.String[],%20int[],%20int%29

A simple fix would be to add ",0" to the end of the argument list.

夕嗳→ 2024-11-11 09:57:59

如果您尝试在 Android 应用程序中使用现有的 sqlite 数据库,那么您需要做一些准备工作才能使其正常工作。这篇博文详细描述了该过程。

http://www.reigndesign.com/博客/在android应用程序中使用您自己的sqlite数据库/

If you are trying to use an existing sqlite database in your Android application then you need to do some prep work to get it to work properly. This blog post describes the process in detail.

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文