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

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

问题描述

我在使用记事本示例中的某些内容时遇到问题.这是 NotepadCodeLab/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);

这段代码似乎工作正常.但为了清楚起见,我运行了 ADB实用程序并运行 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:

sqlite> .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);

我觉得一切都很好.

现在到我的应用程序,据我所知,它与一些小的变化.我已经简化并简化了我的代码,但是问题依旧.

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 消息:

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:

sqlite> .schema创建表 android_metadata (locale TEXT);CREATE TABLE circles (_id integer primary key autoincrement, sequence整数,半径实数,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 的文档 状态:

Cursor 必须包含一个名为 _id 的列,否则此类将不会工作.

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.

Loaders 文档 包含一个示例,从中可以看出_id 包含在 projection 参数中.

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

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

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