问题
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.rawQuery(" SELECT "+ id + " AS _id")
because SimpleAdapter need to have an _id
field necessarily for some reason or it will crash with an error. How do I combine this 2 into one query?
回答1:
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 (boolean distinct, 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 id
from
Org
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"));
}
回答2:
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 = new String[] { id+" AS _id", "Name", "Color", "whatever..." };
Cursor c = db.query("Org", columns, null, null, null, null, null);
来源:https://stackoverflow.com/questions/25752193/simpleadapter-needs-an-id-field