Friday, May 28, 2010

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

When I write a ContentProvider that querying the Sqlite. I using the simple code as bellow. I get this weird the column doesn't not exist error.  what make me confusing that the DBHelper has been worked well for a time.

@Override
    public Cursor query(Uri url, String[] projection, String selection,
            String[] selectionArgs, String sort) {
        //Load all results
        return dbHelpder.getReadableDatabase().query(DBHelper.TABLE_EQ
                , selectionArgs, null, null, null, null, sort);
    }

Also I defined the _ID column.

Code Of DBHelper

@Override
    public void onCreate(SQLiteDatabase paramSQLiteDatabase) {
        String sql="CREATE TABLE tableEQ (_ID INTEGER PRIMARY KEY," +
        " LAT TEXT, " +
        "LON TEXT, " +
        "MAG TEXT, " +
        "Date TEXT, " +
        "Place TEXT," +
        "Depth Text," +
        " ImageMap TEXT, ImageMap2 TEXT)" ;

        paramSQLiteDatabase.execSQL(sql);
    }


How to fix it?
1. Make sure the you have a Column of _id, the column name here is case sensitive. the internal logic use “_id” as a key query to the dictionary.
2. Since the DBCreation Logic once be invoked Once. So either Remove the DB file manually which is located in /data/data/yourappnamespace/databases
or just rename the table name in your sql to reflect the changes. otherwise, you keep polling the Prevous defined DB even you changed your code.

3 comments:

Theah said...

Thanks!

This post was very useful; I was calling it "Id". I didn't realize there was a naming standard you had to follow.

I was passing the Cursor to a CursorAdapter and it didn't like that I didn't have a _id column.

nbalakersnews said...
This comment has been removed by the author.
appdot said...

glad it helps:)

 
Locations of visitors to this page