Home » Android » android – Cursor JOIN with same column name

android – Cursor JOIN with same column name

Posted by: admin June 16, 2020 Leave a comment

Questions:

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

How to&Answers:

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.