Question

how can I access columns with the same name? I have tried doing cursor.getString(cursor.getColumnIndexOrThrow("table.column")) but it doesn't seem to work

Was it helpful?

Solution

You can use aliases, i.e.

db.rawQuery("SELECT column1 AS c1 FROM table");

Then you can use:

cursor.getColumnIndex("c1");

Obviously same applies for fields from JOINs. Cheers.

EDIT

Example with join clause:

db.rawQuery("SELECT t1.columnX AS c1, t2.columnY as c2 FROM table1 t1 INNER JOIN table2 t2 ON t1.A = t2.B");

Then you can simply use the alias name regardless of the table it belongs to:

cursor.getColumnIndex("c2") should return 1.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top