9

Is it possible to get column data by column name instead of index in sqlite? I don't want to rely on a specific order of the columns. If so, what's the syntax?

1 Answer 1

11

You probably want to use Cursor.getColumnIndex() method.

Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use getColumnIndexOrThrow(String) instead, which will make the error more clear.

Example:

String[] columns = new String[]{ COLUMN_XML };
Cursor c = db.query(TABLE, columns, where, whereArgs, null, null, null);
String xml = c.getString(c.getColumnIndex(COLUMN_XML));
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Just needed to think about it differently and nest your solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.