Android的列'_id'不存在吗?不存在、Android、_id

2023-09-12 00:12:04 作者:氣質迷倒尼瑪

我在使用的东西,在记事本示例工作的麻烦。 下面是从记事本codeLAB的code / Notepadv1Solution:

I'm having trouble with something that works in the Notepad example. Here's the code from the NotepadCodeLab/Notepadv1Solution:

String[] from = new String[] { NotesDbAdapter.KEY_TITLE };
int[] to = new int[] { R.id.text1 };

SimpleCursorAdapter notes = new SimpleCursorAdapter(this,
R.layout.notes_row, c, from, to);

这code似乎很好地工作。但仅仅是明确的,我跑了亚行 实用程序并运行的SQLite 3我检查的模式如下:

This code seems to work fine. But just to be clear, I ran the ADB utility and run SQLite 3. I inspected the schema as follows:

源码> .schema

sqlite> .schema

CREATE TABLE android_metadata (locale TEXT);
CREATE TABLE notes (_id integer primary key autoincrement, title text
not null, body text not null);

所有似乎对我好。

All seems good to me.

现在在我的应用程序,其中,据我所看到的,基本上是相同的 一些小的改动。我已经简化并简化了我的code,但 问题仍然存在。

Now on to my application, which, as far as I can see, is basically the same with a few minor changes. I've simplified and simplified my code, but the problem persists.

String[] from = new String[] { "x" };
int[] to = new int[] { R.id.x };

SimpleCursorAdapter adapter = null;
try
{
    adapter = new SimpleCursorAdapter(this, R.layout.circle_row, cursor, from, to);
}
catch (RuntimeException e)
{
    Log.e("Circle", e.toString(), e);
}

当我跑我的应用程序,我得到一个RuntimeException和下面的打印 从我的 Log.e()语句LogCat中:

When I run my application, I get a RuntimeException and the following prints in LogCat from my Log.e() statement:

LogCat中留言:

LogCat Message:

java.lang.IllegalArgumentException:如果列'_id'不存在

java.lang.IllegalArgumentException: column '_id' does not exist

所以,回到SQLite的3,看看有什么关于我的架构不同:

So, back to SQLite 3 to see what's different about my schema:

源码> .schema CREATE TABLE android_metadata(区域TEXT); CREATE TABLE圈(_id整数主键自动增量,序列 整数,半径实,X进行实时,y为实);

sqlite> .schema CREATE TABLE android_metadata (locale TEXT); CREATE TABLE circles (_id integer primary key autoincrement, sequence integer, radius real, x real, y real);

我看不出我错过了'_id'。

I don't see how I'm missing the '_id'.

我做了什么错?

有一件事是不同的我的应用程序和记事本的例子之间 我开始使用从头创建我的应用程序 Eclipse的向导,而示例应用程序自带已经放在一起。是 有某种环境变化我需要一个新的应用程序 使用SQLite数据库?

One thing that's different between my application and the Notepad example is that I started by creating my application from scratch using the Eclipse wizard while the sample application comes already put together. Is there some sort of environmental change I need to make for a new application to use a SQLite database?

推荐答案

我看到的,文档的CursorAdapter 规定:

光标必须包括一个名为 _id A柱或该类不会   的工作。

The Cursor must include a column named _id or this class will not work.

SimpleCursorAdapter 是一个派生类,所以会出现这种说法适用。然而,该语句是技术上的错误,有点误导新手。在结果集作为光标必须包含 _id ,而不是光标本身。 我敢肯定,这是明确的,以一名DBA,因为那种速记文档是明确地给他们,但对于那些新手,不完整的语句会导致混乱。光标是像迭代器和指针,它们仅仅包含了一种机制,横切数据,它们不包含列本身。

The SimpleCursorAdapter is a derived class, so it appears this statement applies. However, the statement is technically wrong and somewhat misleading to a newbie. The result set for the cursor must contain _id, not the cursor itself. I'm sure this is clear to a DBA because that sort of shorthand documentation is clear to them, but for those newbies, being incomplete in the statement causes confusion. Cursors are like iterators or pointers, they contain nothing but a mechanism for transversing the data, they contain no columns themselves.

的装载机文档包含一个例子,其中可以看出,该 _id 包含在预测参数。

The Loaders documentation contains an example where it can be seen that the _id is included in the projection parameter.

static final String[] CONTACTS_SUMMARY_PROJECTION = new String[] {
    Contacts._ID,
    Contacts.DISPLAY_NAME,
    Contacts.CONTACT_STATUS,
    Contacts.CONTACT_PRESENCE,
    Contacts.PHOTO_ID,
    Contacts.LOOKUP_KEY,
};
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    // ...
    return new CursorLoader(getActivity(), baseUri,
            CONTACTS_SUMMARY_PROJECTION, select, null,
            Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");
}