About "_Id" Field in Android SQLite

Android: column '_id' does not exist

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.

column '_id' does not exist using SQLite with android

when first seing the problem i saw here that the answer was to have a string named "_id", i changed it in my table creation file, however a new file was never created, it would have been created on a new device/emulator but on mine it still used the one i have created.

create a new database file by simply changing its name in the table creation code, and the problem is solved.

edit:

also raising the version number would do the trick

Use id column instead of _id in CursorAdapter


Is it possible to make CursorAdapter use "id" field?

No, but you can use SELECT id AS _id, ... to rename it. What it is named in the database is immaterial — what matters is what it is named in the Cursor.

why does SQLite complain that _id column not found when it's included

Try this:

String[] columnNames = new String[]{AppointmentsDB.KEY_ID};
int[] ids = new int[]{R.id.the_id_of_the_edittext_textview_or_wahtever_you_use_in_row_layout};

Also, unlesss you want to do fancy things, maybe you can simply use a SimpleCursorAdapter like this:

ListAdapter adapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columnNames, ids, CursorAdapter.NO_SELECTION);
setListAdapter(adapter);

I would also recommend using a ListFragment instead of ListActivity so you can reuse the list in other activities or situations (tablets...) if needed.

Do i have to use _ID as a SQlite primary key? and does it have to be an INT? (Android Dev)

You can create and define tables as you wish, but if you will have to use the table via a CursorAdapter that won't work without a tweak.

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

You must always issue a select col1 as _id ... to work.

So it's recommended to use _id in the table schema, and if you like to use by some other name you could this this. select _id, _id as customname ... so select the _id column twice with different names.

Android SQLite _id column problem

Hmm, your SQL code for creating the table looks fine to me (even if you could enclose your column names in quotes just to be sure). But maybe you already have an existing old table which is not upgraded? Just to be sure: increment DB_VERSION and execute again.



Related Topics



Leave a reply



Submit