8

I am getting this error

IllegalArgumentException: column '_id' does not exist

When using a SimpleCursorAdapter to retrieve from my database, and the table does indeed have this _id column. Noticing this a common problem, I have tried to work around it given some of the solutions online but none of them work. This is my cursor query:

SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.quoterow, myCursor, new String[]{"_id", "quote"}, new int[]{R.id.quote});

although I should mention the original did not include the _id column, I added this recently to try and solve the problem. Has anyone got any ideas that might help solve the problem?

Swati Garg
  • 995
  • 1
  • 10
  • 21
MindDrip
  • 201
  • 1
  • 4
  • 14

3 Answers3

15

Your database doesn't have to have a column called '_id' but the SimpleCursorAdaptor does need to have one returned. You can do this with an alias.

An example is that I have a table with columns...

uid,name,number

To query this for a SimpleCursorAdapter, I do this with a database rawQuery...

SELECT uid as _id,name,number FROM MY_TABLE

This works fine and supplies the necessary '_id' column to SimpleCursorAdapter.

EDIT: As far as I understand it the _id field is used as a unique key to make sure the data the cursor handles can be handled correctly by adapters and adapterviews etc.

Look at the data model in the docs for Content Providers.

Using a unique key in 'databases' of whatever kind is pretty much universal practice, and as far as I can tell, the use of the column name '_id' (or '_ID') is simply a way of standardizing and simplifying things across databases, content providers, cursors, adapters etc etc

In short, in order for these various components to work correctly, they need a data column with unique values but they must also 'know' what the name of that column is. They wouldn't 'know', so to speak, that my column name 'uid' is the one they need as opposed to my 'name' and 'number' columns.

Squonk
  • 48,735
  • 19
  • 103
  • 135
  • Hi MisterSquonk, I have changed the code to a rawQuery and am no longer getting the column _id error message. Could you explain why the cursor adapter needs to have this _id explained as I cannot make sense of it in my mind. – MindDrip Apr 29 '11 at 12:42
  • @MindDrip: See the EDIT in my answer. – Squonk Apr 29 '11 at 16:04
  • just i rename my task_id column using alias and problem solved!!! task_id as _id – Rasel Jun 23 '15 at 08:40
0

You either don't have a column "_id" in your table or you are not including it in your query. That is what is causing the exception. You need to fix the following as well:

Your last argument for the CursorAdapter constructor is missing the to column reference for _id.

The int[] argument is an array of view ids to populate with values from the cursor. The String[] argument is an array of column names from a row the cursor points to.

You have to have an equal number of values in the from array as you do the to array. Because the data from the Cursor is being grabbed FROM the Cursor and placed TO the views. If there are not an equal number of values in each array, the adapter throws an exception because it doesn't have the right amount of information to map the data to the views.

Also, according to the JavaDoc for SimpleCursorAdapter, that constructor is deprecated because it causes queries to be executed in the UI thread. Which is bad. Use this one instead:

http://developer.android.com/reference/android/widget/SimpleCursorAdapter.html#SimpleCursorAdapter%28android.content.Context,%20int,%20android.database.Cursor,%20java.lang.String[],%20int[],%20int%29

A simple fix would be to add ",0" to the end of the argument list.

adam
  • 3,888
  • 2
  • 20
  • 15
  • Hi Adam, could you explain this in more detail please, the last arg in cursor adapter is to, why does this need to reference the _id? – MindDrip Apr 28 '11 at 21:16
  • I have tried to use the cursor you suggested but strangely it will not accept the int at the end so I cannot try your suggestion. – MindDrip Apr 28 '11 at 21:22
  • Cool, while I am quite new to android I understand the concept of cursors, and my code does have the correct correlation of from and to with respect to the database column and the view, so I do not think this is the problem.But thank you for you suggestion. – MindDrip Apr 28 '11 at 21:33
  • I'm looking at your posted code example: new String[]{"_id", "quote"}, new int[]{R.id.quote}, you have two "from" and one "to". But yeah, I just reread your exception which is about something else. I added that to the top of my answer – adam Apr 28 '11 at 21:36
  • well, it may be worth pointing out that I have two tables that both contain _id although there variable names are KEY_ROWID and KEY_ROWIDb, but I'm not to sure this would be a problem? – MindDrip Apr 28 '11 at 21:45
0

If you are trying to use an existing sqlite database in your Android application then you need to do some prep work to get it to work properly. This blog post describes the process in detail.

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

vee
  • 720
  • 7
  • 8