From the fine manual:
public Cursor query (String table, String[] columns, String selection, String[] selectionArgs, String groupBy, String having, String orderBy, String limit)
[...]
ReturnsA
Cursor
object, which is positioned before the first entry.
So first you have step c
into the result set and you can use moveToFirst
for that:
c.moveToFirst();
Now you need to extract the rowid from the row that the cursor is pointing at. But getColumnIndex
is for mapping a column name to a position in the row:
Returns the zero-based index for the given column name, or -1 if the column doesn't exist.
You're getting a zero from getColumnIndex
because your KEY_ROWID
is the first column in your SELECT query.
I think you're looking for getLong
if you want to extract a long
from the Cursor
's result set:
long rowid = c.getLong(c.getColumnIndex(KEY_ROWID));
If you know the structure of your query (which you do), you could skip the getColumnIndex
call and just use the known index:
long rowid = c.getLong(0);
And if all you're doing is looking up the rowid, you can SELECT just KEY_ROWID
:
String[] columns = new String[]{KEY_ROWID};
There's no need to pull things out of the database that you're ignoring.