Skip to content Skip to sidebar Skip to footer

Simpleadapter Needs An _id Field

I got this : Cursor c = db.query('Org', null, null, null, null, null, null); which means I choose a table 'Org', but together with this I need to make this : Cursor c = db.rawQuer

Solution 1:

The second parameter of the query function is the list of columns. If you want to rename a column, you cannot just blindy return all columns but have to list the desired columns:

String[] columns = newString[] { id+" AS _id", "Name", "Color", "whatever..." };
Cursorc= db.query("Org", columns, null, null, null, null, null);

Solution 2:

For your statement : Cursor c = db.query("Org", null, null, null, null, null, null); the second parameter is wrong, you shoukd mention the column names in it.

public Cursor query (booleandistinct, String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)

Whereas for,

Cursor c = db.rawQuery(" SELECT "+ id + " AS _id from Org"); 

means that you Select id and create an alias of it using AS into _id, and you are selecting this idfromOrg table. so now you will be able to access the result from this query from the column name _id, and in order to access the result use:

c.moveToFirst();    
while (c.moveToNext())
{
 System.out.println(c.getString(c.getColumnIndex("_id")); 
}

Post a Comment for "Simpleadapter Needs An _id Field"