Android column '_id' does not exist?

2018-12-31 20:52发布

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

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

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.


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

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

LogCat Message:

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

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

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

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

What have I done wrong?

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?

8条回答
泛滥B
2楼-- · 2018-12-31 21:18

Yes , I also change the SELECT string query to fix this issue.

String query = "SELECT t.*,t.id as _id FROM table t "; 
查看更多
人气声优
3楼-- · 2018-12-31 21:19

What solved my issue with this error was that I had not included the _id column in my DB query. Adding that solved my problem.

查看更多
登录 后发表回答