开发者

Android sqlite3 says "no _id" but table definitely has

开发者 https://www.devze.com 2023-04-01 02:14 出处:网络
Somehow while debugging a little Location based android app I get a strange exception. Have a look and share your ideas:

Somehow while debugging a little Location based android app I get a strange exception. Have a look and share your ideas:

I have connected a ListActivity with an sqlite3 database through a ContentProvider. Now always when I start the ListActivity the program dies with an Exception telling me

column '_id' does not exist

(without a table name) for the line where I create the SimpleCursorAdapter object for my ListActivity.

    Cursor cursor = managedQuery(intent.getData(), new String[] {TrackTable.START_LOC, TrackTable.END_LOC}, null, null,
            TrackTable.DEFAULT_SORT_ORDER);
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
  开发者_开发百科          R.layout.gps_track_entry, cursor, new String[] {
                    TrackTable.COLUMN_NAME_TIME_START,
                    TrackTable.COLUMN_NAME_TIME_END
                }, new int[] { R.id.l_start, R.id.l_end });

My database looks like that:

sqlite> .tables
.tables
android_metadata ocation track
sqlite> . schema track
.schema track
CREATE TABLE track (
  _id INTEGER PRIMARY KEY,
  start_location INTEGER,
  end_location INTEGER,
  start_time TEXT,
  end_time TEXT
);
sqlite> .schema location
.schema location
CREATE TABLE location (
  _id INTEGER PRIMARY KEY,
  long TEXT,
  lat TEXT,
  alt TEXT,
  accu TEXT,
  bear TEXT,
  sped TEXT,
  time TEXT);
sqlite> .schema android_metadata
.schema android_metadata
CREATE TABLE android_metadata (locale TEXT);

I mean, it is quite unlikely that the mistake is an Android bug, but it definitely looks like one, because the only table without an _id is the android_metadata. But maybe I wrote something in a strange way, that the VM now thinks I want to use android_metadata and not track, which I actually want to use.


Erich is right above; the problem is that your SELECT query needs to include BaseColumns._ID as part of the projection. Something like:

Cursor cursor = getContentResolver().query(TrackTable.CONTENT_URI, 
        new String[] { TrackTable._ID, TrackTable.COLUMN_NAME_TIME_START,
        TrackTable.COLUMN_NAME_TIME_END }, null, null, null);

BTW, see the javadoc for CursorAdapter:

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

0

精彩评论

暂无评论...
验证码 换一张
取 消