Favolas
Favolas

Reputation: 7243

Android cursor get column index using string

When I do cursor.getColumnIndex(Database._F_A3) this works as expected.

But if I do this

String dbColumn = "Database._F_A3";
cursor.getColumnIndex(dbColumn );

it gives error.

I believe that this is because getColumnIndex() manages whatever is inside the parenthesis as the string to search in the columns.

Is there any way to do what I want?

favolas

Upvotes: 2

Views: 6877

Answers (1)

Lalit Poptani
Lalit Poptani

Reputation: 67286

String dbColumn = "Database._F_A3";
cursor.getColumnIndex(dbColumn );

calling above is completely wrong, because it changes the the name that you declared as static in Database class,

suppose you declared in your Database class as,

public static String _F_A3 = "something";

and now if you call,

String dbColumn = "Database._F_A3";
cursor.getColumnIndex(dbColumn );  // it means you are using Database._F_A3 
                 // as column name which doesn't exist instead of "something"

So, better try using

String dbColumn = Database._F_A3;  // without quotes
cursor.getColumnIndex(dbColumn );

Upvotes: 1

Related Questions